Browse Curriculum
FAANGPrep Sprint
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

Observe how we look back 1 step (single digit) and 2 steps (two digits) to sum up the valid ways.

Click 'Randomize' to try different strings.

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.