forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0076.py
More file actions
34 lines (27 loc) · 885 Bytes
/
0076.py
File metadata and controls
34 lines (27 loc) · 885 Bytes
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
class Solution:
def minWindow(self, s: str, t: str) -> str:
if not s or not t:
return ""
map = collections.Counter(t)
required = len(map)
l = 0
r = 0
bestLeft = 0
bestRight = 0
windowLen = len(s) + 1
for r in range(len(s)):
if s[r] in map:
map[s[r]] -= 1
if map[s[r]] == 0:
required -= 1
while required == 0 and l <= r:
if r - l + 1 < windowLen:
windowLen = r - l + 1
bestLeft = l
bestRight = r
if s[l] in map:
map[s[l]] += 1
if map[s[l]] > 0:
required += 1
l += 1
return "" if windowLen == len(s) + 1 else s[bestLeft: bestRight + 1]