Skip to content

Commit 13083db

Browse files
committed
solution: 0076. Minimum Window Substring
1 parent 7cdad97 commit 13083db

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

Diff for: solutions/solution_0076/__init__.py

+39-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,40 @@
1+
from collections import defaultdict
2+
3+
14
class Solution:
2-
def minWindow(self, s: str, t: str) -> str: ...
5+
def minWindow(self, s: str, t: str) -> str:
6+
if len(s) < len(t):
7+
return ''
8+
9+
t_map: defaultdict[str, int] = defaultdict(int)
10+
t_length = len(t)
11+
12+
for char in t:
13+
t_map[char] += 1
14+
15+
left = 0
16+
result = (0, len(s) + 1)
17+
18+
for right, char in enumerate(s):
19+
if t_map[char] > 0:
20+
t_length -= 1
21+
22+
t_map[char] -= 1
23+
24+
if t_length == 0:
25+
while True:
26+
current_left = s[left]
27+
if t_map[current_left] == 0:
28+
break
29+
30+
t_map[current_left] += 1
31+
left += 1
32+
33+
if right - left < result[1] - result[0]:
34+
result = (left, right)
35+
36+
t_map[s[left]] += 1
37+
t_length += 1
38+
left += 1
39+
40+
return s[result[0] : result[1] + 1] if result[1] != float('inf') else ''

0 commit comments

Comments
 (0)