> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Stamping The Sequence Python Solution

> Tested Python solution for LeetCode 936 with 22 pytest cases. Generate a practice environment with lcpy.

LeetCode 936, [Hard](/catalog/hard). Topics: [String](/catalog/topics/string), [Stack](/catalog/topics/stack), [Greedy](/catalog/topics/greedy), [Queue](/catalog/topics/queue). [View on LeetCode](https://leetcode.com/problems/stamping-the-sequence/description/).

Generate this problem as a practice environment: tested reference solution, 22 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 936   # by problem number
lcpy gen -s stamping_the_sequence   # by problem name
```

## Problem

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.

### Examples

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

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/stamping_the_sequence/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/stamping_the_sequence/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n^2 * m) worst case, where n = len(target) and m = len(stamp); each
    # stamp erases at least one letter, so there are at most n stamps per pass.
    # Space: O(n) for the working copy of target.
    def moves_to_stamp(self, stamp: str, target: str) -> list[int]:
        chars = list(target)
        stamp_len = len(stamp)
        target_len = len(chars)
        moves: list[int] = []
        done = 0
        # [left, right) is the region still holding letters from target; every
        # useful stamp window must intersect it, otherwise it only writes over '?'.
        left, right = 0, target_len
        while done < target_len:
            placed_at = -1
            low = max(0, left - stamp_len + 1)
            high = min(right, target_len - stamp_len + 1)
            for start in range(low, high):
                covered = 0
                for offset, char in enumerate(stamp):
                    current = chars[start + offset]
                    if current == "?":
                        continue
                    if current != char:
                        break
                    covered += 1
                else:
                    if covered:
                        placed_at = start
                        break
            if placed_at < 0:
                return []
            for offset in range(stamp_len):
                if chars[placed_at + offset] != "?":
                    chars[placed_at + offset] = "?"
                    done += 1
            moves.append(placed_at)
            while left < target_len and chars[left] == "?":
                left += 1
            while right > left and chars[right - 1] == "?":
                right -= 1
        moves.reverse()
        return moves
```

## Complexity

| Time | Space |
| - | - |
| O(n^2 \* m) worst case, where n = len(target) and m = len(stamp); each | O(n) for the working copy of target. |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.