-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathStamping The Sequence.py
41 lines (31 loc) · 1006 Bytes
/
Stamping The Sequence.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from typing import List
class Solution:
"""
Time: O(m*(m-n))
Memory: O(m*(m-n))
n - length of stamp
m - length of target
"""
MASK = '?'
def movesToStamp(self, stamp: str, target: str) -> List[int]:
target = list(target)
m, n = len(target), len(stamp)
ans = []
seen = set()
for i in range(m - n + 1):
if not self.equals(stamp, target, i):
continue
for j in reversed(range(i + 1)):
if j in seen:
break
seen.add(j)
if self.equals(stamp, target, j):
ans.append(j)
target[j:j + n] = [self.MASK] * n
return ans[::-1] if target == [self.MASK] * m else []
@classmethod
def equals(cls, stamp: str, target: list, i: int) -> bool:
for j, c in enumerate(stamp):
if target[i + j] not in (c, cls.MASK):
return False
return True