91. Decode Ways
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
Method: Dynamic Programming
The problem asks for the number of ways to decode a string of digits. This suggests that we can break the problem down into smaller, overlapping subproblems, which is a hallmark of dynamic programming.
Solution
Decode Ways in Python
class Solution:
def numDecodings(self, s: str) -> int:
# If the string is empty or starts with '0', no decoding is possible.
if not s or s[0] == '0':
return 0
n = len(s)
# O(1) space dynamic programming
# prev2 stores the number of ways to decode s up to index i-2
# prev1 stores the number of ways to decode s up to index i-1
prev2 = 1 # Corresponds to dp[0] = 1 (empty string)
prev1 = 1 # Corresponds to dp[1] = 1 (for the first character s[0])
for i in range(1, n):
current = 0
# Case 1: Decode s[i] as a single digit (1-9)
if s[i] != '0':
current = prev1
# Case 2: Decode s[i-1]s[i] as a two-digit number (10-26)
# The two-digit number is formed by s[i-1] and s[i]
two_digit = int(s[i-1:i+1])
if 10 <= two_digit <= 26:
current += prev2
# Update pointers for the next iteration
prev2 = prev1
prev1 = current
return prev1 # prev1 holds the result for the entire string
Decode Ways in Java
class Solution {
public int numDecodings(String s) {
// If the string is empty or starts with '0', no decoding is possible.
if (s == null || s.length() == 0 || s.charAt(0) == '0') {
return 0;
}
int n = s.length();
// O(1) space dynamic programming
// prev2 stores the number of ways to decode s up to index i-2
// prev1 stores the number of ways to decode s up to index i-1
int prev2 = 1; // Corresponds to dp[0] = 1 (empty string)
int prev1 = 1; // Corresponds to dp[1] = 1 (for the first character s[0])
for (int i = 1; i < n; i++) {
int current = 0;
// Case 1: Decode s[i] as a single digit (1-9)
if (s.charAt(i) != '0') {
current = prev1;
}
// Case 2: Decode s[i-1]s[i] as a two-digit number (10-26)
char prevChar = s.charAt(i - 1);
char currChar = s.charAt(i);
// Check if the two-digit number is between 10 and 26.
if (prevChar == '1' || (prevChar == '2' && currChar <= '6')) {
current += prev2;
}
// Update pointers for the next iteration
prev2 = prev1;
prev1 = current;
}
return prev1; // prev1 holds the result for the entire string
}
}
Decode Ways in C++
class Solution {
public:
int numDecodings(string s) {
// If the string is empty or starts with '0', no decoding is possible.
if (s.empty() || s[0] == '0') {
return 0;
}
int n = s.length();
// O(1) space dynamic programming
// prev2 stores the number of ways to decode s up to index i-2
// prev1 stores the number of ways to decode s up to index i-1
int prev2 = 1; // Corresponds to dp[0] = 1 (empty string)
int prev1 = 1; // Corresponds to dp[1] = 1 (for the first character s[0])
for (int i = 1; i < n; ++i) {
int current = 0;
// Case 1: Decode s[i] as a single digit (1-9)
if (s[i] != '0') {
current = prev1;
}
// Case 2: Decode s[i-1]s[i] as a two-digit number (10-26)
// Check if the two-digit number is between 10 and 26.
// s[i-1] == '1' covers 10-19.
// s[i-1] == '2' and s[i] <= '6' covers 20-26.
if (s[i-1] == '1' || (s[i-1] == '2' && s[i] <= '6')) {
current += prev2;
}
// Update pointers for the next iteration
prev2 = prev1;
prev1 = current;
}
return prev1; // prev1 holds the result for the entire string
}
};
Decode Ways in TypeScript
function numDecodings(s: string): number {
// If the string is empty or starts with '0', no decoding is possible.
if (s.length === 0 || s[0] === '0') {
return 0;
}
const n = s.length;
// O(1) space dynamic programming
// prev2 stores the number of ways to decode s up to index i-2
// prev1 stores the number of ways to decode s up to index i-1
let prev2: number = 1; // Corresponds to dp[0] = 1 (empty string)
let prev1: number = 1; // Corresponds to dp[1] = 1 (for the first character s[0])
for (let i = 1; i < n; i++) {
let current = 0;
// Case 1: Decode s[i] as a single digit (1-9)
if (s[i] !== '0') {
current = prev1;
}
// Case 2: Decode s[i-1]s[i] as a two-digit number (10-26)
const twoDigitNum = parseInt(s.substring(i - 1, i + 1), 10);
if (twoDigitNum >= 10 && twoDigitNum <= 26) {
current += prev2;
}
// Update pointers for the next iteration
prev2 = prev1;
prev1 = current;
}
return prev1; // prev1 holds the result for the entire string
}
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.