Decode Ways
Write the code, see it run, pass the checks.
The problem
- medium
- Code
Write numDecodings(digits). A message in capital letters was turned into a string of digits, with A as 1, B as 2 and so on up to Z as 26, and nothing between the letters. Return how many ways digits could be read back as letters, modulo 1,000,000,007. A 0 can only be the second digit of 10 or 20, so "0", "06" and "30" give 0. An empty string gives 1 (one way: no letters). For example, numDecodings("226") is 3: BZ, VF and BBF. The checks include 100,000 digits, too long for recursion, so work along the string in a loop.
Your code must pass
- Counts the ways to read the digits
- Handles zeros
- Returns 1 for an empty string
- Is fast enough for 100,000 digits