# [LeetCode 639. Decode Ways II]()
### Daily challenge Jul 10, 2021 (HARD)
>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** 10^9^ + 7.
:::info
**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 " *".
:::
:::info
**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*".
:::
:::info
**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*".
:::
:::warning
**Constraints:**
* 1 <= s.length <= 105
* s[i] is a digit or '*'.
:::
---
### Approach 1 : DP :book:
**`32 ms`**
:::success
dp[i] 代表 s[0 : i-1]中,所有可能的 decode 組合
:::
首先可以先考慮 s[i] 是 `digit` 還是 `*`。
但是不論 s[i] 是何者,我們都需要進行兩個判斷。
>1 直接放入尾端。
>2. 與 s[i-1] 組合
---
>Initial state :
>>dp[0] = 1
>
>>dp[1] = 0, if s[0] == "0"
>dp[1] = 9, if s[0] == "*"
>dp[1] = 1, if s[0] == "1~9"
>s[i] == "*" :
>> 1. 有 9 種可能 (1~9) ---> **`dp[i+1] = 9 * dp[i]`**
>
>> 2. 需要考慮 s[i-1] 的種類:
>>>1. s[i-1] == "1" **:** 有 9 種可能 *(11~19)* ---> **`dp[i+1] += 9 * dp[i-1]`**
>>>2. s[i-1] == "2" **:** 有 6 種可能 *(21~26)* ---> **`dp[i+1] += 6 * dp[i-1]`**
>>>3. s[i-1] == "*" **:** 有 15 種可能 *(11~19) + (21~26)* ---> **`dp[i+1] += 15 * dp[i-1]`**
>s[i] == digit :
>>1. 分為兩種情況 :
>>>1. s[i] == "0" ( 0 不能單獨存在 ) ---> **`dp[i+1] = 0`**
>>>2. s[i] == "1~9" ---> **`dp[i+1] = dp[i]`**
>
>>2. 需要考慮 s[i-1] 的種類 :
>>>1. s[i-1] == "1" : 可能為 10、11、...、19 ---> **`dp[i+1] += dp[i-1]`**
>>>2. s[i-1] == "2" && s[i] == "0~6" : 可能為 20、21、...、26 ---> **`dp[i+1] += dp[i-1]`**
>>>3. s[i-1] == "*" :
>>>>* s[i] == "0~6" : 可能為 10、11、...、16 或 20、21、...、26 ( 2 種可能 ) ---> **`dp[i+1] += 2 * dp[i-1]`**
>>>>* s[i] == "0~9" : 可能為 10、11、...、19 ---> **`dp[i+1] += dp[i-1]`**
```cpp=1
class Solution {
public:
#define MOD 1000000007
int numDecodings(string s) {
long dp[s.size()+1];
dp[0] = 1;
dp[1] = (s[0] == '0') ? 0 : (s[0] == '*') ? 9 : 1;
for(int i=1; i<s.size(); i++){
if(s[i] == '*'){
// just add to the last
dp[i+1] = 9 * dp[i] % MOD;
// combine prev s[] //
if(s[i-1] == '1') dp[i+1] += 9 * dp[i-1]; // 11 ~ 19
else if(s[i-1] == '2') dp[i+1] += 6 * dp[i-1]; // 21 ~ 26
else if(s[i-1] == '*') dp[i+1] += 15 * dp[i-1]; // 1(1) ~ 1(9) + 2(1) ~ 2(6)
dp[i+1] %= MOD;
}
else{ // s[i] = "0" ~ "9"
dp[i+1] = (s[i] == '0') ? 0 : dp[i];
if(s[i-1] == '1') dp[i+1] += (s[i] >= '0' && s[i] <='9') ? dp[i-1] : 0; // 10 ~ 19
else if(s[i-1] == '2') dp[i+1] += (s[i] >= '0' && s[i] <='6') ? dp[i-1] : 0; // 20 ~ 26
else if(s[i-1] == '*') dp[i+1] += (s[i] >= '0' && s[i] <='6' ? 2 : 1) * dp[i-1]; // 10 ~ 19 or 20 ~ 26
dp[i+1] %= MOD;
}
}
return dp[s.size()];
}
};
```