-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminWindowSubstring.py
More file actions
38 lines (27 loc) · 908 Bytes
/
minWindowSubstring.py
File metadata and controls
38 lines (27 loc) · 908 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
35
36
# 76. Minimum Window Substring
# Input: S = "ADOBECODEBANC", T = "ABC"
# Output: "BANC"
# Input: S = "ABAACBAB", T="ABC"
# Output: "ACB"
from collections import Counter
def minWindowSubstring(s, t):
tMap = Counter(t)
curMap = {}
i, j = 0, 0
count = 0
ans = (float('inf'), 0, 0) # ans stores in the form of length, i, j
while j < len(s):
curMap[s[j]] = curMap.get(s[j], 0) + 1
if s[j] in tMap and curMap[s[j]] == tMap[s[j]]:
count += 1
while count == len(tMap):
if j - i + 1 < ans[0]:
ans = (j - i + 1, i, j)
curMap[s[i]] -= 1
if curMap[s[i]] < tMap[s[i]]:
count -= 1
i += 1
j += 1
return "" if ans[0] == float('inf') else s[ans[1]: ans[2]+1]
print(minWindowSubstring("ABAACBAB", "ABC"))
print(minWindowSubstring("ADOBECODEBANC", "ABC"))