0639. Decode Ways II

https://leetcode.com/problems/decode-ways-ii

Description

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 '*'.

ac1:

dp, calculate state at each step

class Solution {
    public int numDecodings(String s) {
        // edge cases
        if (s == null || s.length() == 0 || s.charAt(0) == '0') return 0;

        long res = 1, endWith1 = 0, endWith2 = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);

            if (c == '*') {
                long oneDigit = res * 9;
                long twoDigit = endWith1 * 9 + endWith2 * 6;
                long tmp = (oneDigit + twoDigit) % 1000000007;
                endWith1 = res;
                endWith2 = res;
                res = tmp;
            } else {
                long oneDigit = c == '0' ? 0 : res; // 0 cannot be one digit
                long twoDigit = endWith1 + (c <= '6' ? endWith2 : 0); // if end with 2, current char must <= 6
                long tmp = (oneDigit + twoDigit) % 1000000007;
                endWith1 = c == '1' ? res : 0;
                endWith2 = c == '2' ? res : 0;
                res = tmp;
            }

        }

        return (int) res;
    }
}

ac2:

more complex solution, similar with decode ways 1

class Solution {
    public int numDecodings(String s) {
        // edge cases
        if (s == null || s.length() == 0 || s.charAt(0) == '0') return 0;

        int m = 1000000007;
        long prev2 = 1, prev1 = 1, curr = 0;
        for (int i = 0; i < s.length(); i++) {
            long one = oneDigit(s, i);
            long two = i == 0 ? 0 : twoDigit(s, i);
            one = (one * prev1) % m;
            two = (two * prev2) % m;
            curr = (one + two) % m;
            prev2 = prev1;
            prev1 = curr;
        }

        return (int) curr;
    }

    private int oneDigit(String s, int i) {
        if (s.charAt(i) == '0') return 0;
        if (s.charAt(i) == '*') return 9;
        return 1;
    }

    private int twoDigit(String s, int i) {
        char c1 = s.charAt(i-1), c2 = s.charAt(i);

        if (c1 == '*') {
            if (c2 == '*') return 15;
            if (c2 <= '6') return 2;
            if (c2 > '6') return 1;
        } else if (c1 == '1') {
            if (c2 == '*') return 9;
            else return 1;
        } else if (c1 == '2') {
            if (c2 == '*') return 6;
            if (c2 <= '6') return 1;
            if (c2 > '6') return 0;
        }

        return 0;
    }
}

Last updated