Decode Ways - Problem
Secret Agent Decoder Challenge!

You're a secret agent who has intercepted an encrypted message consisting only of digits. The encryption follows a simple pattern where numbers map to letters: "1" โ†’ 'A', "2" โ†’ 'B', ..., "26" โ†’ 'Z'.

However, there's a twist! ๐Ÿ•ต๏ธ The same encoded string can often be decoded in multiple different ways because some codes overlap. For example:

โ€ข The string "12" could be decoded as "AB" (1โ†’A, 2โ†’B) or as "L" (12โ†’L)
โ€ข The string "226" has 3 ways: "BBF" (2,2,6), "BZ" (2,26), or "VF" (22,6)

Important rules:
โ€ข Only numbers 1-26 are valid codes
โ€ข Leading zeros make a code invalid ("06" is invalid, but "6" is valid)
โ€ข If no valid decoding exists, return 0

Goal: Count the total number of different ways to decode the given string.

Input & Output

example_1.py โ€” Basic case
$ Input: "12"
โ€บ Output: 2
๐Ÿ’ก Note: Two ways to decode: "AB" (1,2) or "L" (12). Both are valid since 1โ‰ค12โ‰ค26.
example_2.py โ€” Multiple possibilities
$ Input: "226"
โ€บ Output: 3
๐Ÿ’ก Note: Three ways: "BBF" (2,2,6), "BZ" (2,26), or "VF" (22,6). All combinations form valid letter codes.
example_3.py โ€” Invalid zero case
$ Input: "06"
โ€บ Output: 0
๐Ÿ’ก Note: No valid decoding exists. "06" is invalid due to leading zero, and we can't decode an empty first part.

Visualization

Tap to expand
Building Blocks for "226"String: 2 2 6226Individual digit blocks (always valid if not 0)Possible Double Blocks:22โœ“ Valid (โ‰ค26)26โœ“ Valid (โ‰ค26)Valid Combinations:1. [2] [2] [6] โ†’ B B F2. [2] [26] โ†’ B Z3. [22] [6] โ†’ V FTotal: 3 different ways to arrange the blocks!DP Logicdp[3] = 1 (base)dp[2] = dp[3] = 1 (take '6')dp[1] = dp[2] + dp[3] = 1 + 1 = 2 ('2' and '26')dp[0] = dp[1] + dp[2] = 2 + 1 = 3 ('2' and '22')Each position asks: "How many ways from here?" = "Ways if I take 1 block" + "Ways if I take 2 blocks"
Understanding the Visualization
1
Start with blocks
Each digit is a potential block of size 1, pairs of digits are blocks of size 2
2
Check validity
Single blocks (1-9) are always valid, double blocks (10-26) are sometimes valid
3
Count combinations
Use DP to count all valid ways to combine blocks to build the complete structure
4
Build solution
Each position's count = ways using single block + ways using double block
Key Takeaway
๐ŸŽฏ Key Insight: This problem has optimal substructure - the number of ways to decode any position depends only on the ways to decode the next 1-2 positions, making it perfect for dynamic programming!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Each position is computed only once and cached, so we do O(n) work total

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Memoization table stores results for up to n different positions, plus recursion stack

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค s.length โ‰ค 100
  • s contains only digits
  • s may contain leading zero(s)
Asked in
Meta 45 Google 38 Amazon 32 Microsoft 28
68.4K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen