forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyyyyyyyyyKim.py
More file actions
37 lines (31 loc) ยท 1.49 KB
/
yyyyyyyyyKim.py
File metadata and controls
37 lines (31 loc) ยท 1.49 KB
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
class Solution:
def minWindow(self, s: str, t: str) -> str:
# ์ฌ๋ผ์ด๋ฉ ์๋์ฐ(์๊ฐ๋ณต์ก๋ O(m+n), ๊ณต๊ฐ๋ณต์ก๋ O(n) : m=len(s), n=len(t))
# ํ์ํ ๋ฌธ์ ๊ฐ์ ๋์
๋๋ฆฌ๋ก ์ ๋ฆฌ
need = {}
for i in t:
need[i] = need.get(i,0) + 1
window = {} # ํ์ฌ ์๋์ฐ์ ํฌํจ๋ ๋ฌธ์ ๊ฐ์ ๋์
๋๋ฆฌ๋ก ์ ๋ฆฌ
have = 0 # ์กฐ๊ฑด์ ๋ง์กฑํ ๋ฌธ์ ์
need_count = len(need) # ์กฐ๊ฑด์ ๋ง์กฑํด์ผํ๋ ๋ฌธ์ ์
min_len = 100001 # ์ต์์๋์ฐ๊ธธ์ด(์ต๋๊ฐ์ ์ด๊ธฐ๊ฐ์ผ๋ก ์ค์ )
answer = ""
left = 0
# ์ค๋ฅธ์ชฝ ํฌ์ธํฐ ์ด๋
for right in range(len(s)):
window[s[right]] = window.get(s[right],0) + 1
# ํด๋น ๋ฌธ์๊ฐ ์กฐ๊ฑด์ ๋ง์กฑํ๋ค๋ฉด have += 1
if s[right] in need and window[s[right]] == need[s[right]]:
have += 1
# ๋ชจ๋ ์กฐ๊ฑด ๋ง์กฑํ๋ฉด ์ผ์ชฝ ๊ณ์ ์ค์ด๊ธฐ
while have == need_count:
# ์ต์ ์๋์ฐ ๊ธธ์ด ๊ฐฑ์
if (right - left + 1) < min_len:
min_len = right - left + 1
answer = s[left:right+1]
# ์๋์ฐ ์ผ์ชฝ ๋ฌธ์ ์ ๊ฑฐ
window[s[left]] -= 1
if s[left] in need and window[s[left]] < need[s[left]]:
have -= 1
left += 1
return answer