LeetCode 91 · Medium
Decode Ways Explained (LeetCode 91)
Updated Dec 1, 2026 · 3 min read
At every digit, ask what the last group can be: the digit alone (if it's 1 to 9) or a pair with the digit before (if it makes 10 to 26). So ways[i] = ways[i-1] when the single digit is valid, plus ways[i-2] when the pair is valid, starting from 1 way for the empty string. Keep only the last two counts for O(n) time and O(1) space.

Key points
- ●A group can never start with 0, so a lone 0 is dead and 06 isn't 6.
- ●Trying every split is about 2^n: a 100-digit string has 99 gaps to cut or not.
- ●ways[i] = ways[i-1] (single digit 1-9) + ways[i-2] (pair 10-26).
- ●The empty string counts as 1 way, which makes the first steps work.
- ●Two variables are enough: O(n) time, O(1) space.

The riddle: Maya's note says 2 2 6
Maya finds a note with three digits: 2 2 6. The code is simple. A is 1, B is 2, and so on up to Z, which is 26. How many ways can she read it?
Most people find two. 2 2 6 is B B F, and 22 6 is V F. But 2 26 works too, which is B Z. So the answer is 3, and that's LeetCode 91, Decode Ways: given a string of digits, count how many ways it can be split into groups that are each a letter. A single digit group must be 1 to 9. A two-digit group must be 10 to 26. No group can start with 0.
Small tests that break the easy idea
Try a few tiny inputs before writing code:
| Input | Readings | Count |
|---|---|---|
| 12 | 1 2 (A B), 12 (L) | 2 |
| 27 | 2 7 (B G); 27 is past Z | 1 |
| 10 | 10 (J); a lone 0 isn't a letter | 1 |
| 06 | none; 0 can't stand alone and 06 isn't 6 | 0 |
| 11106 | 1 1 10 6, 11 10 6 | 2 |
The last one shows why guessing fails. Grab single digits and you get 1, 1, 1, then the 0 is stuck. Grab pairs and 11, 10, 6 happens to work, but only by luck. Both real readings glue the 1 to the 0.
The slow way: try every split
Every gap between two digits is either a cut or not. A string of n digits has n - 1 gaps, so there are up to 2^(n-1) ways to cut it. With 100 digits that's about 2^99 splits. Even ten 1s already have 89 valid readings. Checking each one is exponential.
The idea: what can the last group be?
Ask one question at every position: what is the last group? There are only two options.
- The digit alone, if it's 1 to 9. What's left is the string one digit shorter.
- A pair with the digit before, if the two digits make 10 to 26. What's left is the string two digits shorter.
Either way, what's left is a shorter string you've already counted. So:
ways[i] = ways[i-1] (if the single digit is valid) + ways[i-2] (if the pair is valid)
The empty string counts as 1 way, the base case. The visual sheet above builds this rule in Steps 1 to 3.
Trace it on 2 2 6 and 1 1 1 0 6
| Prefix | Single valid? | Pair valid? | ways |
|---|---|---|---|
| (empty) | 1 | ||
| 2 | yes, +1 | 1 | |
| 22 | yes, +1 | 22, +1 | 2 |
| 226 | yes, +2 | 26, +1 | 3 |
For 1 1 1 0 6 the counts go 1, 1, 2, 3. At the 0 the single digit is dead, but 10 is a valid pair, so the count is the one from two back: 2. The 6 then carries that 2, and 06 isn't a pair. The answer is 2.
The code in Python
The table only ever looks back two places, so two variables are enough:
def num_decodings(s):
if s[0] == "0": return 0
prev2, prev1 = 1, 1
for i in range(1, len(s)):
cur = prev1 if s[i] != "0" else 0
if 10 <= int(s[i-1:i+1]) <= 26: cur += prev2
prev2, prev1 = prev1, cur
return prev1
prev2 is the count two back and prev1 the count one back. After each digit, slide them forward. On "226" this returns 3, and on "11106" it returns 2.
Complexity
- Time: O(n), one pass over the digits.
- Space: O(1), two counts.
From about 2^n splits down to a single pass.
The trap to watch for
Zeros. A leading 0 means the answer is 0 right away. A 0 in the middle only survives inside 10 or 20, so "100" has 0 ways: 10 uses the first 0 and the second 0 has nothing to pair with. And remember the range check is 10 to 26, not 1 to 26 for pairs, or "06" will wrongly count as 6.
Frequently asked questions
Why does 226 have 3 decodings and not 2?
Most people find 2 2 6 (B B F) and 22 6 (V F) and stop. The third is 2 26 (B Z). The dynamic programming table finds it for you: the counts go 1, 1, 2, 3.
How does Decode Ways handle zeros?
A 0 can never be a group on its own, and no group may start with 0. So a 0 only survives as the second digit of 10 or 20. At a 0 the single-digit term adds nothing, and only the pair term can carry the count forward. If neither works, the count becomes 0 and stays there.
Is Decode Ways like Climbing Stairs?
Yes. Both add the answer one step back and two steps back. Decode Ways adds conditions: you take the one-step term only if the digit is 1 to 9, and the two-step term only if the pair is 10 to 26. With a string of all 1s it gives the Fibonacci numbers, for example 89 ways for ten 1s.
What is the time complexity of Decode Ways?
O(n) time, since each digit is checked once, and O(1) space, since you keep only the previous two counts. Trying every split instead is about O(2^n).