File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ 풀이 : 슬라이딩 윈도우 이용
3+ 딕셔너리를 이용해 문자의 등장 빈도를 확인, 증가, 감소시킨다
4+ t에 존재하는 모든 문자의 빈도보다 s의 빈도가 같거나 커질 때까지 right를 증가시킨다
5+ left를 증가시키며 s[left]의 문자빈도가 t보다 낮아지는 지점을 찾는다
6+ 기존의 min_total길이보다 짧으면 ans에 left, right 정보를 이용해 update
7+
8+ 알고달레 참조 및 좀 더 최적화 필요
9+
10+ s의 길이 : n
11+
12+ """
13+
14+ class Solution :
15+ def minWindow (self , s : str , t : str ) -> str :
16+ left , right = 0 , 0
17+ min_total = len (s )
18+ s_appear = {}
19+ t_appear = {}
20+ ans = ""
21+ for char in set (t ):
22+ s_appear [char ] = 0
23+ t_appear [char ] = t .count (char )
24+ while (right < len (s )):
25+ if s [right ] in s_appear :
26+ s_appear [s [right ]] += 1
27+ if all (s_appear [char ] >= t_appear [char ] for char in t_appear .keys ()):
28+ while left < right :
29+ if s [left ] in s_appear :
30+ if s_appear [s [left ]] == t_appear [s [left ]]:
31+ break
32+ s_appear [s [left ]] -= 1
33+ left += 1
34+ if (right - left + 1 ) < min_total :
35+ min_total = right - left + 2
36+ ans = s [left :right + 1 ]
37+ right += 1
38+ return ans
You can’t perform that action at this time.
0 commit comments