-
Notifications
You must be signed in to change notification settings - Fork 57
/
Solution.py
68 lines (61 loc) · 2.19 KB
/
Solution.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
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).
Example:
Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC"
Note:
If there is no such window in S that covers all characters in T, return the empty string "".
If there is such window, you are guaranteed that there will always be only one unique minimum window in S.
"""
from collections import Counter
class Solution:
def minWindow(self, s: str, t: str) -> str:
### method 1, the complexity does not meet requirement, but AC
# counter = Counter(t)
# track = dict((k, []) for k in counter)
# num = len(t)
# indices = []
# mlen = None
# start = None
# for i, c in enumerate(s):
# if c not in counter:
# continue
# if len(track[c]) == counter[c]:
# track[c].pop(0)
# track[c].append(i)
# else:
# track[c].append(i)
# num -= 1
# if num == 0:
# midx = None
# for ls in track.values():
# if midx is None or midx > ls[0]:
# midx = ls[0]
# if mlen is None or mlen > (i - midx + 1):
# mlen = i - midx + 1
# start = midx
# return s[start:start + mlen] if mlen else ''
### method 2, using idea of two pointer
if not s or not t:
return ''
need, missing = Counter(t), len(t)
mlen, start, mstart = None, 0, 0
for i, c in enumerate(s):
if c not in need:
continue
need[c] -= 1
if need[c] >= 0:
missing -= 1
if missing > 0:
continue
while start <= i and (s[start] not in need or need[s[start]] < 0):
if s[start] in need:
need[s[start]] += 1
start += 1
clen = i - start + 1
if mlen is None or mlen > clen:
mlen = clen
mstart = start
if mlen is None:
return ''
return s[mstart:mstart + mlen]