forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8804who.py
More file actions
34 lines (27 loc) · 909 Bytes
/
8804who.py
File metadata and controls
34 lines (27 loc) · 909 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
from collections import Counter, defaultdict
class Solution:
def minWindow(self, s: str, t: str) -> str:
answer = ''
counter = Counter(t)
now = defaultdict()
start = 0
end = 0
now[s[start]] = 1
while start<=end and end < len(s):
enough = True
for key in counter.keys():
if key not in now or now[key] < counter[key]:
enough = False
if enough:
if answer == '' or len(answer) > end-start+1:
answer = s[start:end+1]
now[s[start]] -= 1
start += 1
else:
end += 1
if end == len(s):
break
if s[end] not in now:
now[s[end]] = 0
now[s[end]] += 1
return answer