-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
36 lines (28 loc) · 1.01 KB
/
Copy pathsolution.py
File metadata and controls
36 lines (28 loc) · 1.01 KB
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
class Solution:
def minWindow(self, s: str, t: str) -> str:
if not t or not s:
return ""
need = {}
for char in t:
need[char] = need.get(char, 0) + 1
have = {}
required = len(need)
formed = 0
left = 0
best_len = float("inf")
best_left, best_right = 0, 0
for right in range(len(s)):
char = s[right]
have[char] = have.get(char, 0) + 1
if char in need and have[char] == need[char]:
formed += 1
while formed == required:
if right - left + 1 < best_len:
best_len = right - left + 1
best_left, best_right = left, right
left_char = s[left]
have[left_char] -= 1
if left_char in need and have[left_char] < need[left_char]:
formed -= 1
left += 1
return s[best_left:best_right + 1] if best_len != float("inf") else ""