Decode Ways
Decode Ways is LeetCode 91 (Medium). A message of capital letters was encoded as digits with A → 1, B → 2, …, Z → 26. Given the digit string s, return the number of ways to decode it back into letters.
0maps to no letter, so it can only be read as the second digit of10or20.- A two-digit code cannot start with
0:06is not the letter for 6. - Return 0 if no decoding exists.
shas up to 100 digits, and the answer fits in a 32-bit integer.
- 1 <= s.length <= 100
- s contains only digits and may contain leading zero(s).
Intuition
Look at how the last letter of a decoding was made. It used either the last digit alone or the last two digits together. So the ways to decode the first i + 1 digits are:
- the ways for the first
idigits, if digitiis1to9, plus - the ways for the first
i − 1digits, if digitsi − 1andiread10to26.
No decoding is counted twice, because the two groups differ in the length of their last code. Each count needs only the two counts before it, so two variables are enough instead of a table.
Counting the ways to split a sequence into pieces of length 1 or 2, where each piece has its own validity rule. The count at each position depends only on the two positions before it.
Approach
Before reading on, decide what the count for the first i + 1 digits depends on. Then list exactly when a single digit and when a pair of digits can be a letter. Aim for O(n) time and O(1) space.
Reject a leading zero
If s[0] is '0', return 0. No letter's code starts with 0, so the first digit cannot be read at all, and no later digit can repair that.
Start with one way for the empty prefix and for the first digit
Set two_back = 1 and one_back = 1. The empty prefix has exactly one decoding, the empty message, which is what lets a valid first pair such as 26 add its own way.
Add the one-digit and the two-digit choice
For each i from 1, start here at 0. Add one_back if s[i] is not '0', and add two_back if s[i-1:i+1] is between 10 and 26. The range check rejects both 27 and 06.
Shift the two counts forward
Set two_back, one_back = one_back, here and move on. If here is 0, no decoding of this prefix exists, and every later count stays 0, so a zero that cannot pair, as in 30, ends with an answer of 0.
Code in Python, C++ and Java
Common pitfalls
Checking only the upper bound of a pair
if int(s[i - 1 : i + 1]) <= 26:
if 10 <= int(s[i - 1 : i + 1]) <= 26:
A pair that starts with 0, such as 06, has a value below 10 and is not a letter code. On "106" the only decoding is 10, 6, so the answer is 1, but the bug also accepts 06 and returns 2.
Letting a 0 stand alone
here += one_back
if s[i] != "0":
here += one_backNo letter has the code 0, so a single 0 adds no ways. On "10" the answer is 1 (just J), but without the check the lone 0 adds a second, impossible decoding.
Recursing without remembering results
def ways(i):
...
return ways(i + 1) + ways(i + 2)keep only the last two counts, or memoize ways(i)
The same suffix is solved again and again, so the number of calls grows like the answer itself. A string of 45 ones has about 1.8 · 10⁹ decodings, still inside 32 bits, and plain recursion makes about that many calls.
Edge cases
"2101"At the 0, the single digit adds nothing and only the pair 10 counts, so here equals two_back. The answer is 1 (2, 10, 1).
Complexity
two_back, one_back and here are stored; a full table of n + 1 counts would make it O(n) space.Decode Ways FAQ
Why does the count look like Fibonacci?
When every single digit and every pair is valid, as in "1111", each count is the sum of the two before it, which is exactly the Fibonacci rule: 1, 2, 3, 5. Zeros and pairs above 26 remove one of the two terms, which is what breaks the pattern.
What changes when the string can contain *?
Decode Ways II (LeetCode 639) allows it: a * can stand for any digit from 1 to 9. The same two choices apply, but each one is multiplied by how many digits or pairs the * could form, and the answer is taken modulo 10⁹ + 7.