Menu
CoddyTech

Decode Ways

A message of capital letters was encoded with A = 1, B = 2, and so on up to Z = 26, and the codes were written with no separators. Given the digit string s, return how many messages could have produced it.

A letter is read from one digit or two adjacent digits, and a code never starts with 0: 06 is not 6, and a lone 0 is not a letter. If no reading works, return 0.

Function

numDecodings(s: string) → integer
sstring
the digit string to decode
Returnsinteger
the number of letter messages that encode to s

Constraints

  • 1 ≤ s.length ≤ 100
  • s holds only the digits 0 to 9, and it may start with 0.
  • Every prefix and every suffix of s has fewer than 231 readings, so the answer and every count you build on the way fit in a signed 32-bit integer.

Examples

Input
s = "2611"
Output
4
Explanation
The four readings are 2 6 1 1 (BFAA), 26 1 1 (ZAA), 2 6 11 (BFK) and 26 11 (ZK). The middle digits never pair, because 61 is larger than 26.
challenge icon

Follow-up

What if s may also hold *, standing for any digit from 1 to 9? Can you count the readings in O(n) time, modulo 10^9+7?

Reset code
def numDecodings(s):
    # Write code here

Test #1

Test #2

Test #3

Argument

s = "2611"

Expected Output

4