1 Star 0 Fork 0

yuhang2__2/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
stamping-the-sequence.py 1.28 KB
一键复制 编辑 原始数据 按行查看 历史
# Time: O((n - m) * m)
# Space: O((n - m) * m)
import collections
class Solution(object):
def movesToStamp(self, stamp, target):
M, N = len(stamp), len(target)
q = collections.deque()
lookup = [False]*N
result = []
A = []
for i in xrange(N-M+1):
made, todo = set(), set()
for j, c in enumerate(stamp):
if c == target[i+j]:
made.add(i+j)
else:
todo.add(i+j)
A.append((made, todo))
if todo:
continue
result.append(i)
for m in made:
if lookup[m]:
continue
q.append(m)
lookup[m] = True
while q:
i = q.popleft()
for j in xrange(max(0, i-M+1), min(N-M, i)+1):
made, todo = A[j]
if i not in todo:
continue
todo.discard(i)
if todo:
continue
result.append(j)
for m in made:
if lookup[m]:
continue
q.append(m)
lookup[m] = True
return result[::-1] if all(lookup) else []
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yuhang2__2/LeetCode-Solutions.git
git@gitee.com:yuhang2__2/LeetCode-Solutions.git
yuhang2__2
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助