-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path76.py
28 lines (25 loc) · 1017 Bytes
/
76.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
# https://neetcode.io/problems/minimum-window-with-characters
# https://leetcode.com/problems/minimum-window-substring/description/
from collections import defaultdict
class Solution:
def minWindow(self, s: str, t: str) -> str:
scount, tcount = defaultdict(int), defaultdict(int)
for c in t:
tcount[c] += 1
matches = 0
l, r = 0, 0
res = (float('inf'), float('inf'), float('inf'))
print(tcount)
while r < len(s):
if matches != len(tcount):
scount[s[r]] += 1
if s[r] in tcount and scount[s[r]] == tcount[s[r]]:
matches += 1
r += 1
while matches == len(tcount):
res = min(res, (r - l, l, r))
if s[l] in tcount and scount[s[l]] == tcount[s[l]]:
matches -= 1
scount[s[l]] -= 1
l += 1
return s[res[1] : res[2]] if res[0] != float('inf') else ""