-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowString.py
More file actions
49 lines (48 loc) · 1.55 KB
/
Copy pathWindowString.py
File metadata and controls
49 lines (48 loc) · 1.55 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
# https://www.interviewbit.com/problems/window-string/
class Solution:
# @param A : string
# @param B : string
# @return a strings
def minWindow(self, A, B):
if not A or not B or len(A) < len(B):
return ""
target = dict()
for elem in B:
if elem not in target:
target[elem] = 0
target[elem] += 1
curDict = dict()
l = 0
r = 0
numFound = 0
result = ""
while r < len(A):
elem = A[r]
if elem in target:
if elem not in curDict:
curDict[elem] = 1
numFound += 1
else:
if curDict[elem] < target[elem]:
numFound += 1
curDict[elem] += 1
if numFound == len(B):
if result == "":
result = A[l : r + 1]
while True:
elem = A[l]
if elem in target:
cnt = curDict[elem] - 1
curDict[elem] -= 1
if r - l + 1 < len(result):
result = A[l : r + 1]
l += 1
if cnt < target[elem]:
numFound -= 1
while l < r and A[l] not in target:
l += 1
break
else:
l += 1
r += 1
return result