639. Decode Ways II LeetCode Solution
In this guide, you will get 639. Decode Ways II LeetCode Solution with the best time and space complexity. The solution to Decode Ways II problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Decode Ways II solution in C++
- Decode Ways II solution in Java
- Decode Ways II solution in Python
- Additional Resources

Problem Statement of Decode Ways II
A message containing letters from A-Z can be encoded into numbers using the following mapping:
‘A’ -> “1”
‘B’ -> “2”
…
‘Z’ -> “26”
To decode an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example, “11106” can be mapped into:
“AAJF” with the grouping (1 1 10 6)
“KJF” with the grouping (11 10 6)
Note that the grouping (1 11 06) is invalid because “06” cannot be mapped into ‘F’ since “6” is different from “06”.
In addition to the mapping above, an encoded message may contain the ‘*’ character, which can represent any digit from ‘1’ to ‘9’ (‘0’ is excluded). For example, the encoded message “1*” may represent any of the encoded messages “11”, “12”, “13”, “14”, “15”, “16”, “17”, “18”, or “19”. Decoding “1*” is equivalent to decoding any of the encoded messages it can represent.
Given a string s consisting of digits and ‘*’ characters, return the number of ways to decode it.
Since the answer may be very large, return it modulo 109 + 7.
Example 1:
Input: s = “*”
Output: 9
Explanation: The encoded message can represent any of the encoded messages “1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, or “9”.
Each of these can be decoded to the strings “A”, “B”, “C”, “D”, “E”, “F”, “G”, “H”, and “I” respectively.
Hence, there are a total of 9 ways to decode “*”.
Example 2:
Input: s = “1*”
Output: 18
Explanation: The encoded message can represent any of the encoded messages “11”, “12”, “13”, “14”, “15”, “16”, “17”, “18”, or “19”.
Each of these encoded messages have 2 ways to be decoded (e.g. “11” can be decoded to “AA” or “K”).
Hence, there are a total of 9 * 2 = 18 ways to decode “1*”.
Example 3:
Input: s = “2*”
Output: 15
Explanation: The encoded message can represent any of the encoded messages “21”, “22”, “23”, “24”, “25”, “26”, “27”, “28”, or “29”.
“21”, “22”, “23”, “24”, “25”, and “26” have 2 ways of being decoded, but “27”, “28”, and “29” only have 1 way.
Hence, there are a total of (6 * 2) + (3 * 1) = 12 + 3 = 15 ways to decode “2*”.
Constraints:
1 <= s.length <= 105
s[i] is a digit or '*'.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
639. Decode Ways II LeetCode Solution in C++
class Solution {
public:
int numDecodings(string s) {
constexpr int kMod = 1'000'000'007;
const int n = s.length();
// dp[i] := the number of ways to decode s[i..n)
vector<long> dp(n + 1);
dp.back() = 1;
dp[n - 1] = count(s[n - 1]);
for (int i = n - 2; i >= 0; --i) {
dp[i] += count(s[i], s[i + 1]) * dp[i + 2];
dp[i] += count(s[i]) * dp[i + 1];
dp[i] %= kMod;
}
return dp[0];
}
private:
int count(char c) {
if (c == '*')
return 9;
return c != '0';
}
int count(char c1, char c2) {
if (c1 == '*' && c2 == '*') // c1c2: [11-19, 21-26]
return 15;
if (c1 == '*') {
if ('0' <= c2 && c2 <= '6') // c1: [1-2]
return 2;
else // c1: [1]
return 1;
}
if (c2 == '*') {
if (c1 == '1') // c2: [1-9]
return 9;
if (c1 == '2') // c2: [1-6]
return 6;
return 0;
}
return c1 == '1' || (c1 == '2' && c2 <= '6');
}
};
/* code provided by PROGIEZ */
639. Decode Ways II LeetCode Solution in Java
class Solution {
public int numDecodings(String s) {
final int kMod = 1_000_000_007;
final int n = s.length();
// dp[i] := the number of ways to decode s[i..n - 1]
long[] dp = new long[n + 1];
dp[n] = 1;
dp[n - 1] = count(s.charAt(n - 1));
for (int i = n - 2; i >= 0; --i) {
dp[i] += count(s.charAt(i), s.charAt(i + 1)) * dp[i + 2];
dp[i] += count(s.charAt(i)) * dp[i + 1];
dp[i] %= kMod;
}
return (int) dp[0];
}
private int count(char c) {
if (c == '*')
return 9;
return c == '0' ? 0 : 1;
}
private int count(char c1, char c2) {
if (c1 == '*' && c2 == '*')
return 15; // c1c2: [11-19, 21-26]
if (c1 == '*') {
if ('0' <= c2 && c2 <= '6')
return 2; // c1: [1-2]
else
return 1; // c1: [1]
}
if (c2 == '*') {
if (c1 == '1')
return 9; // c2: [1-9]
if (c1 == '2')
return 6; // c2: [1-6]
return 0;
}
return (c1 == '1' || (c1 == '2' && c2 <= '6')) ? 1 : 0;
}
}
// code provided by PROGIEZ
639. Decode Ways II LeetCode Solution in Python
class Solution {
public:
int numDecodings(string s) {
constexpr int kMod = 1'000'000'007;
const int n = s.length();
long prev2 = 1;
long prev1 = count(s[n - 1]);
for (int i = n - 2; i >= 0; --i) {
long dp = count(s[i], s[i + 1]) * prev2 + count(s[i]) * prev1;
dp %= kMod;
prev2 = prev1;
prev1 = dp;
}
return prev1;
}
private:
int count(char c) {
if (c == '*')
return 9;
return c != '0';
}
int count(char c1, char c2) {
if (c1 == '*' && c2 == '*') // c1c2: [11-19, 21-26]
return 15;
if (c1 == '*') {
if ('0' <= c2 && c2 <= '6') // c1: [1-2]
return 2;
else // c1: [1]
return 1;
}
if (c2 == '*') {
if (c1 == '1') // c2: [1-9]
return 9;
if (c1 == '2') // c2: [1-6]
return 6;
return 0;
}
return c1 == '1' || (c1 == '2' && c2 <= '6');
}
};
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.