Decode Ways II Problem


Description

LeetCode Problem 639.

A message containing letters from A-Z can be encoded into numbers using the following mapping:

1
2
3
4
'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 10^9 + 7.

Example 1:

1
2
3
4
5
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:

1
2
3
4
5
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:

1
2
3
4
5
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 <= 10^5
  • s[i] is a digit or ‘*’.


Sample C++ Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Solution {
public:
    int numDecodings(string s) {
        long prev = 1;
        long cur = numOfOneChar(s[0]);
        for (int i = 1; i < s.size(); ++i) {
            long next = prev * numOfTwoChars(s[i - 1], s[i]) + cur * numOfOneChar(s[i]);
            prev = cur;
            cur = next % 1000000007;
        }
        return cur;
    }
    
    int numOfTwoChars(char a, char b) {
        // Return number of char encoded by two digits or '*'.
        if (a == '*') {
            if (b == '*') {
                return 15;
            } else if (b <= '6') {
                return 2;
            } else {
                return 1;
            }
        } else if (a == '1') {
            return b == '*' ? 9 : 1;
        } else if (a == '2') {
            if (b == '*') {
                return 6;
            } else if (b <= '6') {
                return 1;
            } else {
                return 0;
            }
        } 
        return 0;
    }
    
    int numOfOneChar(char a) {
        if (a == '0') {
            return 0;
        } else if (a == '*') {
            return 9;
        }
        return 1;
    }
};




Related Posts

Valid Permutations For DI Sequence Problem

LeetCode 903. You are given a string s of length...

Tallest Billboard Problem

LeetCode 956. You are installing a billboard and want it...

Sum Of Subarray Minimums Problem

LeetCode 907. Given an array of integers arr, find the...

Stone Game Problem

LeetCode 877. Alice and Bob play a game with piles...

Split Array With Same Average Problem

LeetCode 805. You are given an integer array nums.

Soup Servings Problem

LeetCode 808. There are two types of soup, type A...