-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimum_window_substring.py
More file actions
47 lines (35 loc) · 929 Bytes
/
minimum_window_substring.py
File metadata and controls
47 lines (35 loc) · 929 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
37
38
39
40
41
42
43
44
45
46
47
import collections
def minimum_window_substring(original, check):
if not original:
return ""
start = 0
end = 0
res = ""
while end <= len(original):
window = original[start:end+1]
print(window)
if is_subset(check, window):
# satisfies condition
if not res:
res = window
else:
res = get_smaller(res, window)
start += 1
else:
end += 1
return res
# TODO: break ties
def is_subset(check, window):
ccounter = collections.Counter(check)
wcounter = collections.Counter(window)
for kc in ccounter:
if kc not in wcounter:
return False
if ccounter[kc] > wcounter[kc]:
return False
return True
def get_smaller(s1, s2):
if len(s1) == len(s2):
return min(s1, s2)
else:
return min(s1,s2,key=len)