-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimum_Window_Substring.py
More file actions
77 lines (52 loc) · 1.68 KB
/
Copy pathMinimum_Window_Substring.py
File metadata and controls
77 lines (52 loc) · 1.68 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
For example,
S = "ADOBECODEBANC"
T = "ABC"
Minimum window is "BANC".
Note:
If there is no such window in S that covers all characters in T, return the empty string "".
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
"""
from collections import defaultdict
class Solution:
# @return a string
def minWindow(self, S, T):
s_len = len(S)
t_len = len(T)
# store count per char in t
needed = defaultdict(int)
for i in range(t_len):
needed[T[i]] += 1
# total count of chars in S that are already in T
count = 0
# keep track of min windows so far
min_len = float("inf")
min_index = 0
# left boundries for window
start = 0
# advance end pointer for 0 to lenght of S
# each char will be visited at most twice once
# by start and another by end => O(2n) = O(n)
for end in range(s_len):
needed[S[end]] -= 1
# check if S[end] is actually needed to match T
if needed[S[end]] >= 0:
count += 1
# while the window contains all chars in T
while count == t_len:
# update min window
if end - start + 1 < min_len:
min_len = end - start + 1
min_index = start
# move start pointer one step
needed[S[start]] += 1
if needed[S[start]] > 0: # S[start] was needed
count -= 1
start += 1
if min_len == float("inf"): # not found
return ""
return S[min_index: min_index + min_len]
s = Solution()
print s.minWindow("ADOBECODEBANC", "ABC")
#print s.minWindow("acbbaca", "aba")