LeetCode //C - 639. Decode Ways II

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 1 0 9 + 7 10^9 + 7 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 . l e n g t h < = 1 0 5 1 <= s.length <= 10^5 1<=s.length<=105
  • s[i] is a digit or ‘*’.

From: LeetCode
Link: 639. Decode Ways II


Solution:

Ideas:

1. DP Transition:

  • Let dp[i] be the number of ways to decode s[0…i].
  • We update dp[i] based on:
    • The single character decode (s[i] by itself).
    • The two-character decode (s[i-1]s[i] as a pair).
  • dp[i] depends only on dp[i-1] and dp[i-2], so we use two variables to optimize space.

2. Handling ‘*’ cases:

  • If s[i] == ‘*’, it represents any of 1-9, so dp[i] += 9 * dp[i-1].
  • If s[i-1] == ‘*’:
    • *s[i] can be 11-19 (9 ways) and 21-26 (6 ways).
  • If s[i] == ‘’ and s[i-1] == '’:
    • ** can be 11-19 (9 ways) and 21-26 (6 ways), so 15 additional ways.

3. Modulo Constraint:

  • Since numbers can be large, results are taken modulo 1 0 9 + 7 10^9 + 7 109+7.
Code:
#define MOD 1000000007

int numDecodings(char* s) {
    int n = strlen(s);
    if (n == 0) return 0;
    
    // Two DP variables
    long long prev2 = 1, prev1 = (s[0] == '*') ? 9 : (s[0] == '0' ? 0 : 1);
    
    for (int i = 1; i < n; i++) {
        long long curr = 0;
        
        // Single character decode
        if (s[i] == '*') {
            curr = (prev1 * 9) % MOD;
        } else if (s[i] != '0') {
            curr = prev1;
        }

        // Two-character decode
        if (s[i - 1] == '*') {
            if (s[i] == '*') {
                curr = (curr + prev2 * 15) % MOD; // "**" can be "11-19" (9) + "21-26" (6)
            } else if (s[i] >= '0' && s[i] <= '6') {
                curr = (curr + prev2 * 2) % MOD;  // "*0-6" can be "10-16", "20-26"
            } else {
                curr = (curr + prev2) % MOD;      // "*7-9" can only be "17-19"
            }
        } else if (s[i - 1] == '1') {
            if (s[i] == '*') {
                curr = (curr + prev2 * 9) % MOD;  // "1*" can be "11-19"
            } else {
                curr = (curr + prev2) % MOD;      // "1x" valid for all digits
            }
        } else if (s[i - 1] == '2') {
            if (s[i] == '*') {
                curr = (curr + prev2 * 6) % MOD;  // "2*" can be "21-26"
            } else if (s[i] <= '6') {
                curr = (curr + prev2) % MOD;      // "2x" valid for x=0-6
            }
        }

        // Update DP variables
        prev2 = prev1;
        prev1 = curr;
    }

    return (int) prev1;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Navigator_Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值