Algorithm


Problem Name: 639. 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 '*'.

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


int numDecodings(char* s) {
    char p, t;
    long long a, b, c;
    
    p = '0';    // previous character
    a = 0;      // previous previous number
    b = 1;      // previous number
    c = 0;      // current number
    
    while (t = *(s ++)) {
        switch (t) {
            case '0':
                if (p == '*') {
                    c = a * 2;
                } else if (p != '1' && p != '2') {
                    return 0;
                } else {
                    c = a;
                }
                break;
            case '*':
                c = b * 9;
                if (p == '1') {
                    c += a * 9;
                } else if (p == '2') {
                    c += a * 6;
                } else if (p == '*') {
                    c += a * 15;
                }
                break;
            default:
                c = b;
                if (p == '*') {
                    if (t  < = '6') {
                        c += a * 2;
                    } else {
                        c += a;
                    }
                } else if (p == '1' ||
                    (p == '2' && t >= '0' && t  < = '6')) {
                    c += a;
                }
                break;
        }
        a = b % MOD;
        b = c % MOD;
        p = t;
    }
    
    return c % MOD;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
s = "*"

Output

x
+
cmd
9

#2 Code Example with C++ Programming

Code - C++ Programming


class Solution {
public:
    int numDecodings(string s) {
        if(s.empty() || s.front() == '0') return 0;
        int mod = pow(10, 9) + 7;
        long p1 = (s[0] == '*') ? 9 : 1, p2 = 1, t;
        for(int i = 1; i < s.size(); i++){
            if(s[i] == '0') p1 = 0;
            
            if(s[i - 1] != '*' && s[i] != '*'){
                if(s[i - 1] == '1' || s[i - 1] == '2' && s[i]  <  '7'){
                    p1 = p1 + p2;
                    p2 = p1 - p2;
                }
                else p2 = p1;
            }
            else{
                if(s[i - 1] == '*' && s[i] == '*'){
                    t = p1;
                    p1 = p1 * 9 + p2 * (9 + 6);
                    p2 = t;
                }
                else if(s[i] == '*'){
                    if(s[i - 1] == '1'){
                        t = p1;
                        p1 = p1 * 9 + p2 * 9;
                        p2 = t;
                    }
                    else if(s[i - 1] == '2'){
                        t = p1;
                        p1 = p1 * 9 + p2 * 6;
                        p2 = t;
                    }else{
                        t = p1;
                        p1 = p1 * 9;
                        p2 = t;
                    }
                }
                else{
                    if(s[i] == '0'){
                        t = p1;
                        p1 = p2 * 2;
                        p2 = t;
                    }
                    else if(s[i]  <  '7'>{
                        t = p1;
                        p1 = p1 + p2 * 2;
                        p2 = t;
                    }
                    else{
                        p1 = p1 + p2;
                        p2 = p1 - p2;
                    }
                }
            }
            p1 = p1 % mod;
        }
        return p1;
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
s = "*"

Output

x
+
cmd
9

#3 Code Example with Javascript Programming

Code - Javascript Programming


const numDecodings = function(s) {
  const mod = Math.pow(10, 9) + 7
  const dp = [1, s.charAt(0) === '*' ? 9 : s.charAt(0) === '0' ? 0 : 1]
  for (let i = 0; i  <  s.length; i++) {
    if (s.charAt(i) === '*') {
      if (s.charAt(i - 1) === '*') {
        dp[i + 1] = 9 * dp[i] + 15 * dp[i - 1]
      } else if (s.charAt(i - 1) === '1') {
        dp[i + 1] = 9 * dp[i] + 9 * dp[i - 1]
      } else if (s.charAt(i - 1) === '2') {
        dp[i + 1] = 9 * dp[i] + 6 * dp[i - 1]
      } else {
        dp[i + 1] = dp[i] * 9
      }
    } else {
      let mul = s.charAt(i) === '0' ? 0 : 1
      if (s.charAt(i - 1) === '*') {
        dp[i + 1] = mul * dp[i] + (s.charAt(i) <= '6' ? 2 : 1) * dp[i - 1]
      } else if (
        s.charAt(i - 1) === '1' ||
        (s.charAt(i - 1) === '2' && s.charAt(i) <= '6')
      ) {
        dp[i + 1] = mul * dp[i] + dp[i - 1]
      } else {
        dp[i + 1] = mul * dp[i]
      }
    }
    dp[i + 1] = dp[i + 1] % mod
  }
  return dp[dp.length - 1]
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
s = "*"

Output

x
+
cmd
9

#4 Code Example with Python Programming

Code - Python Programming


class Solution:
    def numDecodings(self, s):
        if s[0] == "0": return 0
        dp1 = dp2 = 1
        if s[0] == "*": dp2 = 9
        for i in range(1, len(s)):
            couple, newDp1 = s[i -1: i + 1], dp2
            if s[i] == "0":
                if s[i - 1] == "0" or s[i - 1] >= "3": return 0
                dp2 = 2 * dp1 if s[i - 1] == "*" else dp1
            elif s[i] == "*":
                dp2 *= 9
                if s[i - 1] == "2": dp2 += 6 * dp1
                elif s[i - 1] == "1": dp2 += 9 * dp1
                elif s[i - 1] == "*": dp2 += 15 * dp1
            elif "10" <= couple <= "26": dp2 += dp1
            elif s[i - 1] == "*": dp2 += 2 * dp1 if s[i] <= "6" else dp1
            dp1 = newDp1
        return dp2 % (10 ** 9 + 7)  
Copy The Code & Try With Live Editor

Input

x
+
cmd
s = "1*"

Output

x
+
cmd
18
Advertisements

Demonstration


Previous
#638 Leetcode Shopping Offers Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#640 Leetcode Solve the Equation Solution in C, C++, Java, JavaScript, Python, C# Leetcode