- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 46 lines of Python from the credited upstream file 936.py.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution:2 def movesToStamp(self, stamp: str, target: str) -> list[int]:3 def stampify(s: int) -> int:4 """5 Stamps target[i..i + |stamp|) and returns the number of newly stamped6 characters.7 e.g. stampify("abc", "ababc", 2) returns 3 because target becomes "ab***".8 """9 stampified = len(stamp)10 11 for i, st in enumerate(stamp):12 if target[s + i] == '*': 13 stampified -= 114 elif target[s + i] != st: 15 return 016 17 for i in range(s, s + len(stamp)):18 target[i] = '*'19 20 return stampified21 22 ans = []23 target = list(target)24 25 stamped = [False] * len(target)26 stampedCount = 0 27 28 while stampedCount < len(target):29 isStamped = False30 31 for i in range(len(target) - len(stamp) + 1):32 if stamped[i]:33 continue34 stampified = stampify(i)35 if stampified == 0:36 continue37 stampedCount += stampified38 isStamped = True39 stamped[i] = True40 ans.append(i)41 42 if not isStamped:43 return []44 45 return ans[::-1]46