forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 6
/
minimum-window-substring.py
49 lines (41 loc) · 1.72 KB
/
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
48
49
# Time: O(n)
# Space: O(k), k is the number of different characters
#
# 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 emtpy string "".
#
# If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
#
class Solution:
# @return a string
def minWindow(self, S, T):
current_count = [0 for i in xrange(52)]
expected_count = [0 for i in xrange(52)]
for char in T:
expected_count[ord(char) - ord('a')] += 1
i, count, start, min_width, min_start = 0, 0, 0, float("inf"), 0
while i < len(S):
current_count[ord(S[i]) - ord('a')] += 1
if current_count[ord(S[i]) - ord('a')] <= expected_count[ord(S[i]) - ord('a')]:
count += 1
if count == len(T):
while expected_count[ord(S[start]) - ord('a')] == 0 or\
current_count[ord(S[start]) - ord('a')] > expected_count[ord(S[start]) - ord('a')]:
current_count[ord(S[start]) - ord('a')] -= 1
start += 1
if min_width > i - start + 1:
min_width = i - start + 1
min_start = start
i += 1
if min_width == float("inf"):
return ""
return S[min_start:min_start + min_width]
if __name__ == "__main__":
print Solution().minWindow("ADOBECODEBANC", "ABC")