Decode Ways

medium1-D Dynamic ProgrammingDP over one- and two-digit decodingsB75Solve on LeetCode#91

Part of Practice: Strings & Coins

A message of letters was encoded by replacing each letter with its alphabet position: 'A' became "1", 'B' became "2", ... 'Z' became "26". You receive the resulting digit string s and must count how many different letter messages could have produced it.

The catch is ambiguity: "12" could be 'L' (12) or 'A'+'B' (1, 2). Groupings must be valid codes — "0" matches no letter, so a 0 must be the second digit of a "10" or "20", and codes like "06" are invalid even though "6" is fine.

Return the number of possible decodings (0 if the string cannot be decoded at all).

Constraints: 1 <= s.length <= 100, digits only. The answer fits in a 32-bit integer.

Examples

Input:
s = "12"
Output:
2

Decodes as "AB" (1 | 2) or "L" (12).

Input:
s = "226"
Output:
3

"BZ" (2 | 26), "VF" (22 | 6), or "BBF" (2 | 2 | 6).

Input:
s = "06"
Output:
0

No letter is "0" or "06", and "6" alone would leave the leading 0 unmatched.

Hints

One at a time — struggle first, peek second.

Your thinking space — verify on LeetCode, then mark it.