forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum-window-subsequence.py
60 lines (53 loc) · 1.72 KB
/
minimum-window-subsequence.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
# Time: O(s * t)
# Space: O(s)
class Solution(object):
def minWindow(self, S, T):
"""
:type S: str
:type T: str
:rtype: str
"""
lookup = [[None for _ in xrange(26)] for _ in xrange(len(S)+1)]
find_char_next_pos = [None]*26
for i in reversed(xrange(len(S))):
find_char_next_pos[ord(S[i])-ord('a')] = i+1
lookup[i] = list(find_char_next_pos)
min_i, min_len = None, float("inf")
for i in xrange(len(S)):
if S[i] != T[0]:
continue
start = i
for c in T:
start = lookup[start][ord(c)-ord('a')]
if start == None:
break
else:
if start-i < min_len:
min_i, min_len = i, start-i
return S[min_i:min_i+min_len] if min_i is not None else ""
# Time: O(s * t)
# Space: O(s)
class Solution2(object):
def minWindow(self, S, T):
"""
:type S: str
:type T: str
:rtype: str
"""
dp = [[None for _ in xrange(len(S))] for _ in xrange(2)]
for j, c in enumerate(S):
if c == T[0]:
dp[0][j] = j
for i in xrange(1, len(T)):
prev = None
dp[i%2] = [None] * len(S)
for j, c in enumerate(S):
if prev is not None and c == T[i]:
dp[i%2][j] = prev
if dp[(i-1)%2][j] is not None:
prev = dp[(i-1)%2][j]
start, end = 0, len(S)
for j, i in enumerate(dp[(len(T)-1)%2]):
if i >= 0 and j-i < end-start:
start, end = i, j
return S[start:end+1] if end < len(S) else ""