-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimumWindowSubstring.py
More file actions
45 lines (35 loc) · 1.2 KB
/
minimumWindowSubstring.py
File metadata and controls
45 lines (35 loc) · 1.2 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
37
38
39
40
41
42
43
44
45
from collections import Counter, defaultdict
def minimunWindowSubstring(s, t):
if not s or not t:
return ""
# Step 1: Required character counts
t_count = Counter(t)
window = defaultdict(int)
have = 0
need = len(t_count) # unique characters needed
res_len = float('inf')
res = [0, 0]
left = 0
for right in range(len(s)):
char = s[right]
window[char] += 1
# If current char meets required count
if char in t_count and window[char] == t_count[char]:
have += 1
# Step 2 and 3: Shrink window if all chars matched
while have == need:
# Update result if it's smaller
if (right - left + 1) < res_len:
res = [left, right + 1]
res_len = right - left + 1
# Pop from left to try and shrink
window[s[left]] -= 1
if s[left] in t_count and window[s[left]] < t_count[s[left]]:
have -= 1
left += 1
l, r = res
return s[l:r] if res_len != float('inf') else ""
# Example
s , t = "ADOBECODEBANC", "ABC"
result = minimunWindowSubstring(s, t)
print(result)