Blind 75
54 / 75Free practice

Decode Ways

Medium · Dynamic programming

Jump to code

A digit string encodes letters with 1 → A through 26 → Z. Count valid decodings. A zero cannot stand alone, and codes cannot begin with zero. The input is nonempty.

Examples

Example 1

s = "12"

Output: 2

Example 2

s = "226"

Output: 3

Constraints

  • 1 <= s.length <= 100
  • s consists of digits
  • The result fits a signed 32-bit integer.
Need a hint?

The final letter uses either one valid digit or two valid digits.

Asked in real interviews

Candidates reported versions of Decode Ways from these company interviews. Some company questions are locked and open with Premium or XP.

Browse all real coding interview questions

Accepted submissions mark this problem complete. Progress stays in this browser.

Run checks 5 examples. Submit checks all 8 cases. A free account runs this problem; no XP needed.
Language
Case 1
Input
[
  "12"
]
Expected
2
Case 2
Input
[
  "226"
]
Expected
3
Case 3
Input
[
  "06"
]
Expected
0
Case 4
Input
[
  "10"
]
Expected
1
Case 5
Input
[
  "27"
]
Expected
1

Drafts are saved per language in this browser when storage is available.