Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

639. Decode Ways II

Hard30.9% Acceptance
StringDynamic Programming
Asked by:
M
Microsoft
ProblemSolutions (4)VideosCompanies (1)Notes

Problem Statement

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 '*'.
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions

Approach

Decode Ways II extends the classic decoding problem by introducing the * wildcard, which can represent digits from 1 to 9. The main challenge is counting all valid decoding combinations while handling multiple possibilities introduced by *.

A common strategy is dynamic programming. Let dp[i] represent the number of ways to decode the substring up to index i. For each position, consider both single-digit and two-digit decoding possibilities. When encountering *, calculate how many valid digits or pairs it can represent (e.g., * alone can map to 9 digits, and combinations like 1* or *2 can represent multiple valid two-digit numbers).

Because the number of combinations grows quickly, results are typically computed modulo 10^9 + 7. The optimized approach uses only the previous states instead of a full array, reducing memory usage while maintaining linear traversal.

This results in an efficient solution with O(n) time complexity while carefully accounting for all wildcard decoding cases.

Complexity

ApproachTime ComplexitySpace Complexity
Dynamic Programming with rolling statesO(n)O(1)
Standard DP arrayO(n)O(n)

Video Solution Available

CrioDo

View all video solutions

Solutions (4)

Dynamic Programming

This approach utilizes a dynamic programming (DP) technique to solve the problem. We define a DP array where dp[i] represents the number of ways to decode the substring of length i. We iterate through the string, updating the DP array based on the current character and its possible pairings with the previous character.

Time Complexity: O(n), where n is the length of the input string. Space Complexity: O(n), due to the dynamic programming array used.

PythonJava
1def numDecodings(s: str) -> int:
2    MOD = 10**9 + 7
3    n = len(s)
4    if n == 0:
5        return 0
6    
7    dp = [0] * (n + 1)
8    dp[0] = 1
9    
10    if s[0] == '*':
11        dp[1] = 9
12    elif s[0] != '0':
13        dp[1] = 1
14    
15    for i in range(2, n + 1):
16        
17        first = s[i-1]
18        second = s[i-2]
19
20        if first == '*':
21            dp[i] = 9 * dp[i-1]
22        elif first != '0':
23            dp[i] = dp[i-1]
24
25        if second == '*':
26            if first == '*':
27                dp[i] = (dp[i] + 15 * dp[i-2]) % MOD
28            elif '0' <= first <= '6':
29                dp[i] = (dp[i] + 2 * dp[i-2]) % MOD
30            else:
31                dp[i] = (dp[i] + dp[i-2]) % MOD
32        elif second == '1':
33            if first == '*':
34                dp[i] = (dp[i] + 9 * dp[i-2]) % MOD
35            else:
36                dp[i] = (dp[i] + dp[i-2]) % MOD
37        elif second == '2':
38            if first == '*':
39                dp[i] = (dp[i] + 6 * dp[i-2]) % MOD
40            elif '0' <= first <= '6':
41                dp[i] = (dp[i] + dp[i-2]) % MOD
42    
43    return dp[n] % MOD

Explanation

This solution introduces a dynamic programming approach. The dp[i] array stores the number of decoding ways for the substring up to the ith character. The initial state is set based on the first character, considering '.' as all digits 1-9. Each subsequent character adjusts dp[i] depending on its value and the preceding character(s), also accounting for the '*' possibilities. All computations are done modulo 10^9 + 7 to limit overflow.

Space Optimized Dynamic Programming

This is a space-optimized version of the dynamic programming solution. Instead of using a full dp array, we keep track of only the last two states needed, thereby reducing space complexity.

Time Complexity: O(n), where n is the length of the input string. Space Complexity: O(1), as we use only constant extra space.

CJavaScript
1#include <string.h>
2
3#define







Video Solutions

Watch expert explanations and walkthroughs

How many LeetCode problems should you solve? #leetcode #techinterview #developer #softwareengineer

CrioDo
0:58304,607 views

Asked By Companies

1 companies
M
Microsoft

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Longest Palindromic SubstringMedium
Regular Expression MatchingHard
Generate ParenthesesMedium
Longest Valid ParenthesesHard
More similar problems

Related Topics

StringDynamic Programming

Problem Stats

Acceptance Rate30.9%
DifficultyHard
Companies1

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Decode Ways II asked in FAANG interviews?

Yes, variations of decoding and dynamic programming problems appear frequently in FAANG-style interviews. Decode Ways II is considered a harder variant due to the wildcard handling and combinatorial counting.

What is the optimal approach for Decode Ways II?

The optimal approach uses dynamic programming to track the number of decoding ways up to each position in the string. Special care is taken to handle the '*' wildcard, which can represent digits from 1 to 9. Optimized implementations reduce space by keeping only the previous states.

Why is the '*' character tricky in Decode Ways II?

The '*' character can represent any digit from 1 to 9, which introduces multiple decoding possibilities at each step. When combined with adjacent characters, it can form several valid two-digit numbers, significantly increasing the number of cases to consider.

What data structure is best for solving Decode Ways II?

A dynamic programming approach using variables or an array is most effective. It tracks the number of valid decodings up to each index while accounting for single-digit and two-digit possibilities involving the wildcard.

MOD
1000000007
4
5
int
numDecodings
(
char
*
s
)
{
6
int
n
=
strlen
(
s
)
;
7
if
(
n
==
0
)
return
0
;
8
9
long
prev
=
1
,
curr
=
s
[
0
]
==
'*'
?
9
:
s
[
0
]
!=
'0'
?
1
:
0
;
10
11
for
(
int
i
=
1
;
i
<
n
;
i
++
)
{
12
long
temp
=
curr
;
13
char
first
=
s
[
i
]
;
14
char
second
=
s
[
i
-
1
]
;
15
16
curr
=
0
;
17
18
if
(
first
==
'*'
)
{
19
curr
=
9
*
temp
;
20
}
else
if
(
first
!=
'0'
)
{
21
curr
=
temp
;
22
}
23
24
if
(
second
==
'*'
)
{
25
if
(
first
==
'*'
)
{
26
curr
=
(
curr
+
15
*
prev
)
%
MOD
;
27
}
else
if
(
first
<=
'6'
)
{
28
curr
=
(
curr
+
2
*
prev
)
%
MOD
;
29
}
else
{
30
curr
=
(
curr
+
prev
)
%
MOD
;
31
}
32
}
else
if
(
second
==
'1'
)
{
33
if
(
first
==
'*'
)
{
34
curr
=
(
curr
+
9
*
prev
)
%
MOD
;
35
}
else
{
36
curr
=
(
curr
+
prev
)
%
MOD
;
37
}
38
}
else
if
(
second
==
'2'
)
{
39
if
(
first
==
'*'
)
{
40
curr
=
(
curr
+
6
*
prev
)
%
MOD
;
41
}
else
if
(
first
<=
'6'
)
{
42
curr
=
(
curr
+
prev
)
%
MOD
;
43
}
44
}
45
46
prev
=
temp
;
47
}
48
49
return
(
int
)
curr
;
50
}

Explanation

This C solution optimizes space by maintaining only two integer variables, prev and curr, to store results of the previous and current positions respectively. This replaces the need for an entire dp array, reducing space usage while maintaining the dynamic programming logic.