Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Decode Ways II

Updated
4 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "639" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

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

Explanation

Here's the breakdown of the solution:

  • Dynamic Programming: The core idea is to use dynamic programming to build up the number of ways to decode the string s from the beginning. dp[i] stores the number of ways to decode the substring s[:i].
  • Base Cases and Transitions: The base cases are dp[0] = 1 (empty string has one way to decode) and dp[1] depending on the first character. The transitions depend on the current character and the previous character, considering all possible valid combinations (single digit and two-digit decoding).
  • Handling '*': The '' character adds complexity. It requires considering all possible digits it can represent (1-9). The transition calculations need to handle '' in both one-digit and two-digit decoding scenarios.

  • Runtime Complexity: O(n), where n is the length of the string s. Storage Complexity: O(n) which can be further optimized to O(1) by only storing previous two values.

Code

    def numDecodings(s: str) -> int:
    n = len(s)
    dp = [0] * (n + 1)
    dp[0] = 1
    dp[1] = ways_one_digit(s[0])

    for i in range(2, n + 1):
        dp[i] = (dp[i] + dp[i - 1] * ways_one_digit(s[i - 1])) % (10**9 + 7)
        dp[i] = (dp[i] + dp[i - 2] * ways_two_digits(s[i - 2], s[i - 1])) % (10**9 + 7)

    return dp[n]

def ways_one_digit(s: str) -> int:
    if s == '*':
        return 9
    elif s == '0':
        return 0
    else:
        return 1

def ways_two_digits(s1: str, s2: str) -> int:
    if s1 == '*' and s2 == '*':
        return 15
    elif s1 == '*':
        if s2 <= '6':
            return 2
        else:
            return 1
    elif s2 == '*':
        if s1 == '1':
            return 9
        elif s1 == '2':
            return 6
        else:
            return 0
    else:
        num = int(s1 + s2)
        if 10 <= num <= 26:
            return 1
        else:
            return 0

More from this blog

C

Chatmagic blog

2894 posts