-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.py
More file actions
34 lines (27 loc) · 881 Bytes
/
Copy pathsolution.py
File metadata and controls
34 lines (27 loc) · 881 Bytes
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
class Solution:
def minWindow(self, s1, s2):
n, m = len(s1), len(s2)
min_len = float('inf')
start = -1
for i in range(n):
if s1[i] != s2[0]:
continue
p1, p2 = i, 0
# Forward scan
while p1 < n and p2 < m:
if s1[p1] == s2[p2]:
p2 += 1
p1 += 1
if p2 == m:
end = p1 - 1
# Backward shrink
p2 = m - 1
while p2 >= 0:
if s1[end] == s2[p2]:
p2 -= 1
end -= 1
end += 1
if p1 - end < min_len:
min_len = p1 - end
start = end
return "" if start == -1 else s1[start:start + min_len]