Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Stamping The Sequence

Updated
3 min read

Introduction

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

You are given two strings stamp and target. Initially, there is a string s of length target.length with all s[i] == '?'. In one turn, you can place stamp over s and replace every letter in the s with the corresponding letter from stamp. For example, if stamp = "abc" and target = "abcba", then s is "?????" initially. In one turn you can: place stamp at index 0 of s to obtain "abc??", place stamp at index 1 of s to obtain "?abc?", or place stamp at index 2 of s to obtain "??abc". Note that stamp must be fully contained in the boundaries of s in order to stamp (i.e., you cannot place stamp at index 3 of s). We want to convert s to target using at most 10 target.length turns. Return an array of the index of the left-most letter being stamped at each turn. If we cannot obtain target from s within 10 target.length turns, return an empty array. Example 1: Input: stamp = "abc", target = "ababc" Output: [0,2] Explanation: Initially s = "?????". - Place stamp at index 0 to get "abc??". - Place stamp at index 2 to get "ababc". [1,0,2] would also be accepted as an answer, as well as some other answers. Example 2: Input: stamp = "abca", target = "aabcaca" Output: [3,0,1] Explanation: Initially s = "???????". - Place stamp at index 3 to get "???abca". - Place stamp at index 0 to get "abcabca". - Place stamp at index 1 to get "aabcaca". Constraints: 1 <= stamp.length <= target.length <= 1000 stamp and target consist of lowercase English letters.

Explanation

Here's the solution to the stamp and target problem:

  • Reverse Operation: Instead of trying to build the target from the initial state of all '?', we work backward. We try to find occurrences of stamp within target that can be "removed" (replaced with '?').
  • Iterative Removal: We iteratively search for removable stamps within target. A stamp is removable if, at a given index, all non-'?' characters match the corresponding characters in the stamp.
  • Recording and Reversing: We record the indices where we successfully remove stamps. Since we're working backward, we reverse the order of these indices at the end to get the correct order of stamping.

  • Runtime Complexity: O(m*n), where n is the length of target and m is the length of stamp.

  • Storage Complexity: O(n), primarily for storing the result array and auxiliary arrays.

Code

    def movesToStamp(stamp: str, target: str) -> list[int]:
    n = len(target)
    m = len(stamp)
    s = list(target)
    res = []
    stamped = [False] * n
    count = 0

    def can_stamp(i):
        change = False
        for j in range(m):
            if s[i + j] == '?':
                continue
            if s[i + j] != stamp[j]:
                return False
            else:
                change = True  # At least one character matches
        return change

    def do_stamp(i):
        nonlocal count
        for j in range(m):
            if s[i + j] != '?':
                s[i + j] = '?'
                count += 1
                stamped[i+j] = True

    while count < n:
        changed = False
        for i in range(n - m + 1):
            if not stamped[i:i+m] == [True]*m and can_stamp(i):
                do_stamp(i)
                res.append(i)
                changed = True

        if not changed:
            return []

    res.reverse()
    return res

More from this blog

C

Chatmagic blog

2894 posts

Solving Leetcode Interviews in Seconds with AI: Stamping The Sequence