Decode Ways
A message is encoded as a string of digits where 'A' = "1", 'B' = "2", ..., 'Z' = "26". Given a digit string, return the number of ways to decode it. A leading zero or an invalid two-digit code (27-99 or 00, 01... but 10 and 20 are valid) means that decoding is impossible through that path.
This is a premium problem.
Unlock Decode Ways with both full courses, DSA and System Design, and every other premium problem.
Problem
A message containing letters from A-Z can be encoded into numbers. Given a string s containing only digits, return the number of ways to decode it.
Input
A string `s` containing only digits.
Output
The number of ways to decode the string. Return 0 if no valid decoding exists.
Examples
Input: s = "12"
Output: 2
Can be decoded as "AB" (1,2) or "L" (12).
Input: s = "226"
Output: 3
"BZ" (2,26), "VF" (22,6), or "BBF" (2,2,6).
Input: s = "06"
Output: 0
"06" cannot be decoded because "0" alone is not valid and "06" is not between 1-26.
The brute-force approach
Recursively try taking 1 digit or 2 digits at a time. If 1 digit is not '0', recurse on the rest. If 2 digits form a valid code (10-26), also recurse on the rest. Sum both results.
def decode(i):
if i == len(s): return 1 # successfully decoded
if s[i] == '0': return 0 # can't decode leading zero
result = decode(i + 1) # take one digit
if i + 1 < len(s):
two = int(s[i:i+2])
if 10 <= two <= 26:
result += decode(i + 2) # take two digits
return result
return decode(0)O(2^n) calls without memoization — the same suffixes are computed repeatedly.
Spotting the pattern
This is a Dynamic Programming problem. The key question to ask yourself:
If dp[i] depends on dp[i+1] and dp[i+2], and you know dp[n]=1, dp[n+1]=0, which direction should you fill the dp array?
Answering that is where it clicks, and it's exactly what the guided walkthrough below builds with you: the pattern reasoning, a progressive hint ladder that never spoils the answer, a row-by-row dry run, the optimized solution, and an in-browser editor to run your code against real test cases.
Unlock the full guided solution
The Dynamic Programming walkthrough for Decode Ways: the progressive hint ladder, a row-by-row dry run, the optimized code, and an in-browser runner, plus both full courses, DSA and System Design, and every other premium problem.
Already finished Big-O and Hash Maps free? This picks up right where the path leads: same format, harder patterns, the ones that show up in every FAANG-style screen.
What you get
- ✓The full DSA course: concept lessons, drills, graded tests, and cold-read capstones for every pattern
- ✓The full System Design course: 20 building-block units for the design round, included at no extra cost
- ✓Stop going blank on algorithm problems. The hint ladder walks you to the answer without spoiling it.
- ✓Actually internalize the trace: dry runs walk state row by row so it clicks before you write a line
- ✓Know when to reach for each pattern. Every problem names the trigger so you spot it next time.
- ✓Write and run JS or Python in the browser with real test cases on every problem
- ✓Access every new problem, unit, and pattern guide as they ship weekly
“This app finally made it click. I tried NeetCode, CTCI, and YouTube for years and still couldn't solve Two Sum. The beginner mental models for the patterns are genuinely so helpful. THANK YOU.”
Full access
$49 / year
or $9 / month
Now includes a C++ runner alongside JavaScript and Python.
LeetCode Premium is $159/year and doesn't teach you anything.
Less than one Udemy course, with guided problems instead of passive lectures.
One extra month of job searching costs more than a full year here.
- ✓Both courses, every unit: DSA + System Design
- ✓All 150 problems across 33 patterns, plus every new one
- ✓New problems and units weekly
- ✓Cancel anytime
Cancel anytime. Not useful within 7 days? Email for a full refund. Secured by Stripe.