-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumWindowSubstring.py
More file actions
65 lines (61 loc) · 1.6 KB
/
MinimumWindowSubstring.py
File metadata and controls
65 lines (61 loc) · 1.6 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
#!/usr/bin/env python
# encoding: utf-8
"""
@version: 1.0
@author: Ding4it
@license: Apache Licence
@contact: ding4it@gmail.com
@file: MinimumWindowSubstring.py
@time: 2016/1/1 19:03
"""
class Solution(object):
def minWindow(self, s, t):
"""
:type s: str
:type t: str
:rtype: str
"""
m = len(t)
n = len(s)
visited = dict()
for chr in t:
if chr not in visited:
visited[chr] = 0
else:
visited[chr] -= 1
start = 0
end = 0
min_start = 0
min_end = n+1
min_len = n+1
print(min_start,min_end)
for end in range(n):
chr = s[end]
if chr not in visited:
continue
if visited[chr] <= 0:
m -= 1
visited[chr] += 1
while m == 0:
tmp = s[start]
if tmp not in visited:
start += 1
elif visited[tmp] > 1:
start += 1
visited[tmp] -= 1
elif visited[tmp] == 1:
if end - start < min_len:
min_start = start
min_end = end+1
min_len = end - start+1
print(min_start,min_end)
start += 1
visited[tmp] -= 1
m += 1
print(min_start,min_end)
if min_len == n+1:
return ""
else:
return s[min_start:min_end]
A = Solution()
A.minWindow("a","aa")