-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path76.minimum-window-substring.py
37 lines (32 loc) · 1.01 KB
/
76.minimum-window-substring.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
#
# @lc app=leetcode id=76 lang=python3
#
# [76] Minimum Window Substring
#
# @lc code=start
# TAGS: Hash Table, Two Pointers, String, Sliding Window
import collections
class Solution:
# 140 ms, 34.23%. Time and Space: O(T+S)
def minWindow(self, s: str, t: str) -> str:
counter = collections.Counter(t)
seen = collections.Counter()
match = set()
window = "." * (len(s) + 1)
ptr = 0
for i, c in enumerate(s): # O(S)
seen[c] += 1
# This char is in t
if c in counter and seen[c] >= counter[c]:
match.add(c)
while len(match) == len(counter):
# Get minium window
if i + 1 - ptr < len(window):
window = s[ptr: i + 1]
# Move left ptr
seen[s[ptr]] -= 1
if seen[s[ptr]] < counter[s[ptr]]:
match.discard(s[ptr])
ptr += 1
return "" if window[0] == '.' else window
# @lc code=end