Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Check if an Original String Exists Given Two Encoded Strings

Updated
4 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2060" 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

An original string, consisting of lowercase English letters, can be encoded by the following steps: Arbitrarily split it into a sequence of some number of non-empty substrings. Arbitrarily choose some elements (possibly none) of the sequence, and replace each with its length (as a numeric string). Concatenate the sequence as the encoded string. For example, one way to encode an original string "abcdefghijklmnop" might be: Split it as a sequence: ["ab", "cdefghijklmn", "o", "p"]. Choose the second and third elements to be replaced by their lengths, respectively. The sequence becomes ["ab", "12", "1", "p"]. Concatenate the elements of the sequence to get the encoded string: "ab121p". Given two encoded strings s1 and s2, consisting of lowercase English letters and digits 1-9 (inclusive), return true if there exists an original string that could be encoded as both s1 and s2. Otherwise, return false. Note: The test cases are generated such that the number of consecutive digits in s1 and s2 does not exceed 3. Example 1: Input: s1 = "internationalization", s2 = "i18n" Output: true Explanation: It is possible that "internationalization" was the original string. - "internationalization" -> Split: ["internationalization"] -> Do not replace any element -> Concatenate: "internationalization", which is s1. - "internationalization" -> Split: ["i", "nternationalizatio", "n"] -> Replace: ["i", "18", "n"] -> Concatenate: "i18n", which is s2 Example 2: Input: s1 = "l123e", s2 = "44" Output: true Explanation: It is possible that "leetcode" was the original string. - "leetcode" -> Split: ["l", "e", "et", "cod", "e"] -> Replace: ["l", "1", "2", "3", "e"] -> Concatenate: "l123e", which is s1. - "leetcode" -> Split: ["leet", "code"] -> Replace: ["4", "4"] -> Concatenate: "44", which is s2. Example 3: Input: s1 = "a5b", s2 = "c5b" Output: false Explanation: It is impossible. - The original string encoded as s1 must start with the letter 'a'. - The original string encoded as s2 must start with the letter 'c'. Constraints: 1 <= s1.length, s2.length <= 40 s1 and s2 consist of digits 1-9 (inclusive), and lowercase English letters only. The number of consecutive digits in s1 and s2 does not exceed 3.

Explanation

Here's the solution to the problem:

  • High-Level Approach:

    • Use dynamic programming to determine if two substrings of s1 and s2 can be derived from the same original string.
    • The DP state dp[i][j] represents whether s1[i:] and s2[j:] can be encoded from the same original string.
    • The transitions consider either matching characters directly or using a number to represent the length of a substring in the original string.
  • Complexity:

    • Runtime Complexity: O(m*n), where m and n are the lengths of s1 and s2, respectively.
    • Storage Complexity: O(m*n)

Code

    def is_encoded_string(s1: str, s2: str) -> bool:
    """
    Given two encoded strings s1 and s2, consisting of lowercase English letters and digits 1-9 (inclusive),
    return true if there exists an original string that could be encoded as both s1 and s2. Otherwise, return false.
    """
    n1 = len(s1)
    n2 = len(s2)

    dp = {}

    def solve(i: int, j: int) -> bool:
        if (i, j) in dp:
            return dp[(i, j)]

        if i == n1 and j == n2:
            return True
        if i == n1 or j == n2:
            return False

        if s1[i].isalpha() and s2[j].isalpha():
            if s1[i] == s2[j]:
                dp[(i, j)] = solve(i + 1, j + 1)
                return dp[(i, j)]
            else:
                dp[(i, j)] = False
                return False

        if s1[i].isdigit():
            len1 = 0
            k = i
            while k < n1 and s1[k].isdigit() and len1 < 3:
                len1 += 1

            num1 = int(s1[i:i + len1])


            if s2[j].isalpha():
                dp[(i, j)] = False
            else:
                len2 = 0
                k = j
                while k < n2 and s2[k].isdigit() and len2 < 3:
                    len2 += 1
                num2 = int(s2[j:j+len2])

                if num1 == num2:
                    dp[(i, j)] = solve(i + len1, j + len2)

                else:
                    dp[(i, j)] = False

            if dp[(i, j)] == True:
                 return True


            original_string_possible_1 = False
            if s2[j].isalpha() == False:

                original_string_possible_1 = solve(i + len1, j + num1)

            dp[(i, j)] = original_string_possible_1
            if dp[(i, j)] == True:
                    return True


        if s2[j].isdigit():
            len2 = 0
            k = j
            while k < n2 and s2[k].isdigit() and len2 < 3:
                len2 += 1

            num2 = int(s2[j:j + len2])

            original_string_possible_2 = False

            if s1[i].isalpha() == False:
                original_string_possible_2 = solve(i + num2, j + len2)

            dp[(i, j)] = original_string_possible_2
            if dp[(i, j)] == True:
                return True


        dp[(i, j)] = False
        return False

    return solve(0, 0)

More from this blog

C

Chatmagic blog

2894 posts