-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0076-minimum-window-substring.py
More file actions
48 lines (40 loc) · 1.29 KB
/
0076-minimum-window-substring.py
File metadata and controls
48 lines (40 loc) · 1.29 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
46
47
48
# time complexity: O(len(s) + len(t))
# space complexity: O(len(s) + len(t))
from collections import defaultdict
class Solution:
def minWindow(self, s: str, t: str) -> str:
feqCount = defaultdict(int)
window = defaultdict(int)
result = [-1, -1]
resultLen = float('inf')
current = 0
for char in t:
feqCount[char] += 1
required = len(feqCount)
left = 0
for right in range(len(s)):
char = s[right]
if char in feqCount:
window[char] += 1
if window[char] == feqCount[char]:
current += 1
while current == required:
if (right - left + 1) < resultLen:
resultLen = right - left + 1
result = [left, right]
leftChar = s[left]
if leftChar in window:
window[leftChar] -= 1
if window[leftChar] < feqCount[leftChar]:
current -= 1
left += 1
return s[result[0]:result[1] + 1] if resultLen != float('inf') else ""
s = "ADOBECODEBANC"
t = "ABC"
print(Solution().minWindow(s, t))
s = "a"
t = "a"
print(Solution().minWindow(s, t))
s = "a"
t = "aa"
print(Solution().minWindow(s, t))