Count how many ways a digit string can be decoded into letters using the mapping 1->A, 2->B, ..., 26->Z.
You are given a string of digits. Each non-empty segment of 1 or 2 digits can represent a letter if it maps to a value from 1 to 26, where 1 corresponds to A and 26 corresponds to Z.
Return the number of different ways to decode the entire string into letters.
A decode is valid only if every chosen segment is within the range 1 to 26 and segments do not start with 0.
Input Format
- A single string
sconsisting only of digits. srepresents the encoded message.
Output Format
- Return an integer: the total number of valid decodings of
s.
Constraints
1 <= s.lengthscontains only characters'0'-'9'- A segment may not start with
0. - Only values from
1to26are allowed.
Example 1
Input
s = "12"
Output
2
Explanation
The string can be decoded as "AB" (1, 2) or "L" (12).
Example 2
Input
s = "226"
Output
3
Explanation
Valid decodings are "BBF" (2,2,6), "BZ" (2,26), and "VF" (22,6).
Show 1 more example
Example 3
Input
s = "06"
Output
0
Explanation
No valid decoding starts with 0.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.