Problem
A simple code changes a message of uppercase letters into a string of digits. Each letter becomes its position in the alphabet: A is 1, B is 2 and Z is 26.
The code writes these numbers one after the other. For example, the message "LAB" becomes "1212".
You get a string s of digits. Return the number of ways to decode s. To decode s, split it into parts so that each part is the number of one letter.
A part cannot start with 0. So "6" and "10" are valid parts, but "06" and "0" are not. If no split is valid, return 0.
Example 1
- Input:
- s = "123"
- Output:
- 3
- Explanation:
- The splits are
1 2 3,12 3and1 23. They decode to"ABC","LC"and"AW".
Example 2
- Input:
- s = "2101"
- Output:
- 1
- Explanation:
- The only valid split is
2 10 1. The0cannot be a part alone, so it must be the end of the part10.
Example 3
- Input:
- s = "30"
- Output:
- 0
- Explanation:
- The number 30 is larger than 26, and
0cannot be a part alone.
Constraints
- 1 <= s.length <= 100
- s has only digits. It can start with 0.
- The answer fits in a 32-bit integer.
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Number of Ways to Form a Target String Given a Dictionary
Coding · Array · Dynamic Programming · String
- Distinct Subsequences
Coding · Dynamic Programming · String
- Generate Parentheses
Coding · Backtracking · Bracket Sequences · Dynamic Programming
- Longest Common Subsequence
Coding · Dynamic Programming · Longest Common Subsequence · String
Sources · 2 references
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 2 records
Anduril
Leetcode 91. Decode Ways
Reported May 1, 2026 · 1 source report
SENIOR
Source snapshot: Nov 12, 2026
View sourceMeta
Leetcode 91. Decode Ways
Reported Aug 22, 2024 · 1 source report
SENIOR
Source snapshot: Nov 12, 2026
View source
LeetCode company tags · 2 records
Anduril
Decode Ways
Reported date not specified · Source window: 6mo
Source snapshot: Oct 20, 2026
Source frequency score: 65.6. This is not an interview probability.
View sourceLyft
Decode Ways
Reported date not specified · Source window: 6mo
Source snapshot: Oct 20, 2026