File tree 1 file changed +41
-6
lines changed
1 file changed +41
-6
lines changed Original file line number Diff line number Diff line change 1
1
"""
2
2
Constraints:
3
- -
3
+ - m == s.length
4
+ - n == t.length
5
+ - 1 <= m, n <= 10^5
6
+ - s and t consist of uppercase and lowercase English letters.
4
7
5
- Time Complexity:
6
- -
8
+ Time Complexity: O(s * t)
9
+ - for ๋ฃจํ๋ก s์ ๊ธธ์ด๋งํผ ๋ฐ๋ณต
10
+ - while ๋ฃจํ ์์ all() ์กฐ๊ฑด์์ t์ ๊ฐ ๋ฌธ์ ๋น๊ต
11
+ - ๋ฐ๋ผ์ O(s * t)
7
12
8
- Space Complexity:
9
- -
13
+ Space Complexity: O(s + t)
14
+ - t_counts: O(t)์ ๊ณต๊ฐ
15
+ - w_counts: O(s)์ ๊ณต๊ฐ
16
+ - ๋ฐ๋ผ์ O(s + t)
10
17
11
18
ํ์ด๋ฐฉ๋ฒ:
12
- 1.
19
+ 1. Counter๋ก t์ ๋ฌธ์ ๋น๋์ ์ ์ฅ
20
+ 2. ์ฌ๋ผ์ด๋ฉ ์๋์ฐ๋ก s ํ์:
21
+ - high ํฌ์ธํฐ๋ก ์๋์ฐ๋ฅผ ํ์ฅํ๋ฉฐ ํ์ํ ๋ฌธ์ ์ฐพ๊ธฐ
22
+ - ํ์ํ ๋ฌธ์๋ฅผ ๋ชจ๋ ์ฐพ์ผ๋ฉด low ํฌ์ธํฐ๋ก ์๋์ฐ ์ถ์
23
+ 3. ์ต์ ๊ธธ์ด์ ์๋์ฐ ๋ฐํ
24
+
25
+ ๋ฉ๋ชจ:
26
+ - ํ์ด ๋ฐฉ๋ฒ์ ๋ณด๊ณ ์ตํ
27
+ - ํด์๋งต๊ณผ ์ฌ๋ผ์ด๋ฉ ์๋์ฐ ๊ด๋ จ ๋ค๋ฅธ ๋ฌธ์ ๋ค์ ํ๊ณ ์ด ๋ฌธ์ ์ค์ค๋ก ํ์ด๋ณด๊ธฐ
13
28
"""
29
+ class Solution :
30
+ def minWindow (self , s : str , t : str ) -> str :
31
+
32
+ t_counts = Counter (t )
33
+ w_counts = Counter ()
34
+
35
+ min_low , min_high = 0 , len (s )
36
+ low = 0
37
+
38
+ for high in range (len (s )):
39
+ w_counts [s [high ]] += 1
40
+
41
+ while all (t_counts [ch ] <= w_counts [ch ] for ch in t_counts ):
42
+ if high - low < min_high - min_low :
43
+ min_low , min_high = low , high
44
+ if s [low ] in t_counts :
45
+ w_counts [s [low ]] -= 1
46
+ low += 1
47
+
48
+ return s [min_low : min_high + 1 ] if min_high < len (s ) else ""
14
49
You canโt perform that action at this time.
0 commit comments