639. Decode Ways II
hardCount the number of ways to decode a string of digits and asterisks, where '*' stands for any digit 1-9. Decode Ways with multipliers — the recursion is the same, the case analysis is much heavier.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
A message containing letters from A-Z can be encoded into numbers using the 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), or '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 'A?' where '?' is a digit between '1' and '9'. 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 10^9 + 7.
Constraints
1 <= s.length <= 10^5s[i] is a digit or '*'.
Examples
Example 1
s = "*"9Explanation: 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
s = "1*"18Explanation: 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
s = "2*"15Explanation: 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*".
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Hints
Progressive — try the first before opening the next.
Hint 1
Recurse: ways(i) = (single-char ways for s[i]) * ways(i + 1) + (two-char ways for s[i..i+1]) * ways(i + 2).
Hint 2
Single-char ways: 0 if s[i] == '0'; 9 if s[i] == '*'; 1 otherwise.
Hint 3
Two-char ways: pattern match (digit/*, digit/*) and count valid combinations decoding to 10..26.
Hint 4
Memoize on i. Take everything mod 10^9 + 7.
Solution approach
Reveal approach
Recursive ways(i): if i == s.length return 1. Base 0: if s[i] == '0' return 0. Otherwise: ways = (single-char count for s[i]) * ways(i + 1). If i + 1 < s.length, ways += (two-char count for s[i..i+1]) * ways(i + 2). Take everything mod 10^9 + 7. Single-char counts: digit '0' -> 0, digit '1'..'9' -> 1, '*' -> 9. Two-char counts (s[i], s[i+1]) -> count of valid 10..26 patterns: ('1','*') -> 9, ('2','*') -> 6 (20..26), ('*','*') -> 15 (11..19 + 21..26 = 15), ('*', digit 0..6) -> 2 (1d or 2d), ('*', digit 7..9) -> 1 (1d only), ('1', digit) -> 1, ('2', '0'..'6') -> 1, others -> 0. Memoize on i. Time and space O(n).
Complexity
- Time
- O(n)
- Space
- O(n)
Related patterns
- recursion
- memoization
- dynamic-programming
Related problems
- 91. Decode Ways
- 70. Climbing Stairs
- 509. Fibonacci Number
Asked at
Companies reported asking this problem (sourced from public Glassdoor, Blind, and Levels.fyi interview posts).
- Meta
- Amazon
Practice these live with InterviewChamp.AI
Drill Decode Ways II and Recursion problems under real interview conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →