Menu
CoddyTech

Decode Ways

A message of capital letters was turned into digits with the code A = 1, B = 2, and so on up to Z = 26, and the codes were written one after another with no separators. You get the digit string s. Return how many different messages could have produced it.

Every letter is read from one digit or from two digits next to each other, and a code never starts with 0: 06 is not 6, and a 0 on its own 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.

lock icon+25 hidden tests on Submit

challenge icon

Follow-up

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

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

Case 1

Case 2

Case 3

Input

s = "2611"

Expected

4