KatInterview

639. Decode Ways II

HardSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

This problem is an extension of "Decode Ways", with the added complexity of a wildcard character '*'. We need to find the total number of ways to decode a string composed of digits and '*'. The '*' can represent any digit from '1' to '9'. This is a classic counting problem that fits the dynamic programming paradigm, as the number of ways to decode a prefix of the string is dependent on the number of ways to decode its smaller prefixes.

Solution

Decode Ways II in Python

class Solution:
    def numDecodings(self, s: str) -> int:
        n = len(s)
        MOD = 10**9 + 7

        # dp[i] depends on dp[i-1] and dp[i-2], so we use space optimization.
        # prev2 represents dp[i-2], prev1 represents dp[i-1]
        prev2 = 1  # Base case for dp[0]
        
        # Calculate dp[1]
        if s[0] == '0':
            prev1 = 0
        elif s[0] == '*':
            prev1 = 9
        else:
            prev1 = 1

        if n == 1:
            return prev1

        for i in range(2, n + 1):
            c1 = s[i - 2]
            c2 = s[i - 1]
            curr = 0

            # Case 1: Decode s[i-1] as a single character
            ways1 = 0
            if c2 == '*':
                ways1 = 9
            elif c2 != '0':
                ways1 = 1
            curr = (prev1 * ways1) % MOD

            # Case 2: Decode s[i-2]s[i-1] as a two-character group
            ways2 = 0
            if c1 == '*':
                if c2 == '*':  # **
                    ways2 = 15  # 11-19 (9) + 21-26 (6)
                elif '0' <= c2 <= '6':  # *[0-6]
                    ways2 = 2  # 1[0-6], 2[0-6]
                else:  # *[7-9]
                    ways2 = 1  # 1[7-9]
            elif c1 == '1':
                if c2 == '*':  # 1*
                    ways2 = 9  # 11-19
                else:  # 1[0-9]
                    ways2 = 1
            elif c1 == '2':
                if c2 == '*':  # 2*
                    ways2 = 6  # 21-26
                elif '0' <= c2 <= '6':  # 2[0-6]
                    ways2 = 1
            
            curr = (curr + prev2 * ways2) % MOD

            prev2 = prev1
            prev1 = curr

        return prev1

Decode Ways II in Java

class Solution {
    public int numDecodings(String s) {
        int n = s.length();
        long MOD = 1_000_000_007;

        // dp[i] depends on dp[i-1] and dp[i-2], so we use space optimization.
        // prev2 represents dp[i-2], prev1 represents dp[i-1]
        long prev2 = 1; // Base case for dp[0]
        long prev1;     // Base case for dp[1]

        // Calculate dp[1]
        if (s.charAt(0) == '0') {
            prev1 = 0;
        } else if (s.charAt(0) == '*') {
            prev1 = 9;
        } else {
            prev1 = 1;
        }

        if (n == 1) {
            return (int) prev1;
        }

        for (int i = 2; i <= n; ++i) {
            char c1 = s.charAt(i - 2);
            char c2 = s.charAt(i - 1);
            long curr = 0;

            // Case 1: Decode s[i-1] as a single character
            long ways1 = 0;
            if (c2 == '*') {
                ways1 = 9;
            } else if (c2 != '0') {
                ways1 = 1;
            }
            curr = (prev1 * ways1) % MOD;

            // Case 2: Decode s[i-2]s[i-1] as a two-character group
            long ways2 = 0;
            if (c1 == '*') {
                if (c2 == '*') { // **
                    ways2 = 15; // 11-19 (9) + 21-26 (6)
                } else if (c2 >= '0' && c2 <= '6') { // *[0-6]
                    ways2 = 2; // 1[0-6], 2[0-6]
                } else { // *[7-9]
                    ways2 = 1; // 1[7-9]
                }
            } else if (c1 == '1') {
                if (c2 == '*') { // 1*
                    ways2 = 9; // 11-19
                } else { // 1[0-9]
                    ways2 = 1;
                }
            } else if (c1 == '2') {
                if (c2 == '*') { // 2*
                    ways2 = 6; // 21-26
                } else if (c2 >= '0' && c2 <= '6') { // 2[0-6]
                    ways2 = 1;
                }
            }
            curr = (curr + prev2 * ways2) % MOD;

            prev2 = prev1;
            prev1 = curr;
        }

        return (int) prev1;
    }
}

Decode Ways II in C++

class Solution {
public:
    int numDecodings(string s) {
        int n = s.length();
        long MOD = 1e9 + 7;

        // dp[i] depends on dp[i-1] and dp[i-2], so we use space optimization.
        // prev2 represents dp[i-2], prev1 represents dp[i-1]
        long prev2 = 1; // Base case for dp[0]
        long prev1;     // Base case for dp[1]

        // Calculate dp[1]
        if (s[0] == '0') {
            prev1 = 0;
        } else if (s[0] == '*') {
            prev1 = 9;
        } else {
            prev1 = 1;
        }

        if (n == 1) {
            return prev1;
        }

        for (int i = 2; i <= n; ++i) {
            char c1 = s[i - 2];
            char c2 = s[i - 1];
            long curr = 0;

            // Case 1: Decode s[i-1] as a single character
            long ways1 = 0;
            if (c2 == '*') {
                ways1 = 9;
            } else if (c2 != '0') {
                ways1 = 1;
            }
            curr = (prev1 * ways1) % MOD;

            // Case 2: Decode s[i-2]s[i-1] as a two-character group
            long ways2 = 0;
            if (c1 == '*') {
                if (c2 == '*') { // **
                    ways2 = 15; // 11-19 (9) + 21-26 (6)
                } else if (c2 >= '0' && c2 <= '6') { // *[0-6]
                    ways2 = 2; // 1[0-6], 2[0-6]
                } else { // *[7-9]
                    ways2 = 1; // 1[7-9]
                }
            } else if (c1 == '1') {
                if (c2 == '*') { // 1*
                    ways2 = 9; // 11-19
                } else { // 1[0-9]
                    ways2 = 1;
                }
            } else if (c1 == '2') {
                if (c2 == '*') { // 2*
                    ways2 = 6; // 21-26
                } else if (c2 >= '0' && c2 <= '6') { // 2[0-6]
                    ways2 = 1;
                }
            }
            curr = (curr + prev2 * ways2) % MOD;

            prev2 = prev1;
            prev1 = curr;
        }

        return prev1;
    }
};

Decode Ways II in TypeScript

function numDecodings(s: string): number {
    const n = s.length;
    const MOD = 1000000007n;

    // Using BigInt for calculations to prevent overflow

    // dp[i] depends on dp[i-1] and dp[i-2], so we use space optimization.
    // prev2 represents dp[i-2], prev1 represents dp[i-1]
    let prev2: bigint = 1n; // Base case for dp[0]
    let prev1: bigint;     // Base case for dp[1]

    // Calculate dp[1]
    if (s[0] === '0') {
        prev1 = 0n;
    } else if (s[0] === '*') {
        prev1 = 9n;
    } else {
        prev1 = 1n;
    }

    if (n === 1) {
        return Number(prev1);
    }

    for (let i = 2; i <= n; ++i) {
        const c1 = s[i - 2];
        const c2 = s[i - 1];
        let curr: bigint = 0n;

        // Case 1: Decode s[i-1] as a single character
        let ways1 = 0n;
        if (c2 === '*') {
            ways1 = 9n;
        } else if (c2 !== '0') {
            ways1 = 1n;
        }
        curr = (prev1 * ways1) % MOD;

        // Case 2: Decode s[i-2]s[i-1] as a two-character group
        let ways2 = 0n;
        if (c1 === '*') {
            if (c2 === '*') { // **
                ways2 = 15n; // 11-19 (9) + 21-26 (6)
            } else if (c2 >= '0' && c2 <= '6') { // *[0-6]
                ways2 = 2n; // 1[0-6], 2[0-6]
            } else { // *[7-9]
                ways2 = 1n; // 1[7-9]
            }
        } else if (c1 === '1') {
            if (c2 === '*') { // 1*
                ways2 = 9n; // 11-19
            } else { // 1[0-9]
                ways2 = 1n;
            }
        } else if (c1 === '2') {
            if (c2 === '*') { // 2*
                ways2 = 6n; // 21-26
            } else if (c2 >= '0' && c2 <= '6') { // 2[0-6]
                ways2 = 1n;
            }
        }
        curr = (curr + prev2 * ways2) % MOD;

        prev2 = prev1;
        prev1 = curr;
    }

    return Number(prev1);
}

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.