-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetcode Problem 76 Minimum Window Substring.txt
76 lines (58 loc) · 2.5 KB
/
Leetcode Problem 76 Minimum Window Substring.txt
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
69
70
71
72
73
74
75
76
76. Minimum Window Substring
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.
Hint to solve: Use two pointer approach. If the substring does not contain all the char in the pattern then increament rightPtr
If the substring contains all the char in the pattern then record the result if the size of the window is less.
- Decreament or delete the key at the left ptr
- Increament the left ptr
class Solution:
def isValidSubstring(self,originalDict, wordDict):
isValid = True
for key in originalDict:
if key not in wordDict:
isValid = False
break
if originalDict[key] > wordDict[key]:
isValid = False
break
return isValid
def minWindow(self, s: str, t: str) -> str:
leftPtr = 0
rightPtr = 0
wordDict = {}
originalDict = {}
for alpha in t:
if alpha in originalDict:
originalDict[alpha] += 1
else:
originalDict[alpha] = 1
wordDict[s[0]] = 1
minWindowSize = sys.maxsize
result = ""
while rightPtr < len(s):
#print(F'leftPtr: {leftPtr} rightPtr: {rightPtr} result:{result} minWindowSize: {minWindowSize}')
if self.isValidSubstring(originalDict, wordDict) == False:
rightPtr += 1
if rightPtr == len(s):
break
if s[rightPtr] in wordDict:
wordDict[s[rightPtr]] += 1
else:
wordDict[s[rightPtr]] = 1
else:
if rightPtr - leftPtr < minWindowSize:
minWindowSize = rightPtr - leftPtr + 1
result = s[leftPtr:leftPtr+minWindowSize]
#print(result)
if s[leftPtr] in wordDict:
if wordDict[s[leftPtr]] == 1:
del wordDict[s[leftPtr]]
else:
wordDict[s[leftPtr]] -= 1
leftPtr += 1
return result