-
Notifications
You must be signed in to change notification settings - Fork 24
/
76 - Minimum Window Substring.py
48 lines (46 loc) · 1.47 KB
/
76 - Minimum Window Substring.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from collections import deque
class Solution(object):
def isValidWindow(self,sCount,tCount):
for key in tCount:
if key not in sCount:
return False
if sCount[key] < tCount[key]:
return False
return True
def minWindow(self, s, t):
"""
:type s: str
:type t: str
:rtype: str
"""
if len(t) > len(s):
return ""
# Count all the letters in t
tCount = {}
for c in t:
if c in tCount:
tCount[c] += 1
else:
tCount[c] = 1
# We'll also count the letters in s as we go through the array
sCount = {}
queue = deque([])
minWindow = ""
for i,c in enumerate(s):
if c not in tCount:
continue
if c in sCount:
sCount[c] += 1
else:
sCount[c] = 1
queue.append((c,i))
if self.isValidWindow(sCount,tCount):
# Minimize the window (slide to the right if possible)
pair = None
while len(queue) > 0 and self.isValidWindow(sCount,tCount):
pair = queue.popleft()
sCount[pair[0]] -= 1
# Check if it's the minimum
if minWindow == "" or i-pair[1]+1 < len(minWindow):
minWindow = s[pair[1]:i+1]
return minWindow