Skip to content
Onsite

Decode Ways

MediumP2Coding

A message of letters is encoded with A -> 1, B -> 2, ..., Z -> 26 and the codes are written one after another with no separators. Given the digit string s, return the number of different ways to decode it back into letters.

A code never has a leading zero, so "06" is not a valid code and "0" alone cannot be decoded. If s cannot be decoded at all, return 0.

Example 1

Input
s = "12"
Output
2
Why
"AB" (1 2) or "L" (12).

Example 2

Input
s = "226"
Output
3
Why
"BZ" (2 26), "VF" (22 6) or "BBF" (2 2 6).

Example 3

Input
s = "06"
Output
0

Constraints

  • 1 <= s.length <= 60
  • s contains only digits.
  • The answer fits in a 53-bit integer.

Hints

Run checks the 3 visible tests, Submit all 12.