-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
39 lines (27 loc) · 1.02 KB
/
solution.py
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
import collections
class Solution:
def minWindow(self, s: str, t: str) -> str:
if not t or not s:
return ""
t_count = collections.Counter(t)
have, need = 0, len(t_count)
res, res_len = [None, None], float("inf")
window_count = {}
left, right = 0, 0
while right < len(s):
char = s[right]
window_count[char] = 1 + window_count.get(char, 0)
if char in t_count and window_count[char] == t_count[char]:
have += 1
while left <= right and have == need:
char = s[left]
if right - left + 1 < res_len:
res = [left, right]
res_len = right - left + 1
window_count[char] -= 1
if char in t_count and window_count[char] < t_count[char]:
have -= 1
left += 1
right += 1
left, right = res
return s[left: right+1] if res_len != float("inf") else ""