0
点赞
收藏
分享

微信扫一扫

LeetCode题解(0727):最小窗口子序列(Python)


题目:​​原题链接​​(困难)

标签:动态规划、滑动窗口

解法

时间复杂度

空间复杂度

执行用时

Ans 1 (Python)

1092ms (32.35%)

Ans 2 (Python)

Ans 3 (Python)

解法一:

class Solution:
def minWindow(self, s: str, t: str) -> str:
l, s2 = len(s), len(t)

count = collections.defaultdict(list)
for i, ch in enumerate(s):
count[ch].append(i)

for ch in t:
if ch not in count:
return ""

now = [[i, i] for i in count[t[0]]]

for j in range(1, s2):
place = [i for i, ch in enumerate(s) if ch == t[j]]

nxt = []
for l, r in now:
idx = bisect.bisect_right(place, r)
if idx < len(place):
nxt.append([l, place[idx]])
now = nxt

ans = ""
for l, r in now:
if not ans or r - l + 1 < len(ans):
ans = s[l:r + 1]
return


举报

相关推荐

0 条评论