Dynamic Programming
Medium

Decode Ways

Determine the total number of ways to decode a string of digits into letters (A-Z).
Problem 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:

  1. One Digit: If s[i-1] is '1'-'9', we can add dp[i-1] ways.
  2. Two Digits: If s[i-2...i-1] is "10"-"26", we can add dp[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.
Unlock VisualizerPREMIUM FEATURE

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.

Start Your Premium Prep