Dynamic Programming
Medium
Decode Ways
Determine the total number of ways to decode a string of digits into letters (A-Z).
10 min read
Solve on LeetCodeProblem Understanding
We convert digits to letters: '1' -> 'A', '2' -> 'B', ..., '26' -> 'Z'. Given a string s, finding the count of possible decodings.
Example: s = "12"
- "1, 2" -> "AB"
- "12" -> "L"
- Output: 2
Algorithm Strategy
We use DP. Let dp[i] be the number of ways to decode the prefix s[0...i-1].
Transitions:
- One Digit: If
s[i-1]is '1'-'9', we can adddp[i-1]ways. - Two Digits: If
s[i-2...i-1]is "10"-"26", we can adddp[i-2]ways.
Base case: dp[0] = 1 (empty string has 1 way).
Interactive Visualization
Step 1 / 1
Initializing...
1x
See the Logic in Motion
Stop memorizing code. Unlock the full interactive visualizer to master the logic step-by-step.ON THIS PAGE
- Problem Understanding
- Algorithm Strategy
- Interactive Visualization
- Dry Run
- Edge Cases & Common Mistakes
- Solution
- Complexity Analysis
Stop Guessing, Start Mastering.
Build the FAANG intuition. Master this pattern with optimized implementations, visual dry runs, and our curated collection of high-yield problems.
