Skip to content

Commit 526a7ec

Browse files
committed
Time: 125 ms (47.66%), Space: 17.4 MB (62.89%) - LeetHub
1 parent 5cc0817 commit 526a7ec

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import collections
2+
3+
class Solution:
4+
def minWindow(self, s: str, t: str) -> str:
5+
if not t or not s:
6+
return ""
7+
8+
missing = len(t)
9+
counter = collections.Counter(t)
10+
left = start = end = 0
11+
for right in range(1, len(s) + 1):
12+
if s[right - 1] in counter:
13+
missing -= counter[s[right - 1]] > 0
14+
counter[s[right - 1]] -= 1
15+
16+
if missing == 0:
17+
while left < right and (s[left] not in counter or counter[s[left]] < 0):
18+
if s[left] in counter:
19+
counter[s[left]] += 1
20+
left += 1
21+
22+
if not end or right - left <= end - start:
23+
start, end = left, right
24+
25+
return s[start:end]

0 commit comments

Comments
 (0)