forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkayden.py
More file actions
39 lines (32 loc) ยท 970 Bytes
/
kayden.py
File metadata and controls
39 lines (32 loc) ยท 970 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
35
36
37
38
39
from collections import Counter, deque
class Solution:
# ์๊ฐ๋ณต์ก๋: O(S+T)
# ๊ณต๊ฐ๋ณต์ก๋: O(T)
def minWindow(self, s: str, t: str) -> str:
counter = Counter(t)
index = deque()
tot = 0
m = len(s)
st, en = 0, m - 1
for idx, ch in enumerate(s):
if ch not in counter:
continue
counter[ch] -= 1
index.append(idx)
if counter[ch] == 0:
tot += 1
while index:
if counter[s[index[0]]] < 0:
counter[s[index[0]]] += 1
index.popleft()
else:
break
if tot == len(counter):
a = index[0]
b = idx
if b - a + 1 < m:
st, en = a, b
m = en - st + 1
if tot != len(counter):
return ""
return s[st:en + 1]