Skip to content

Commit 5140ffd

Browse files
committed
solve: Minimum Window Substring
1 parent 08e7b1d commit 5140ffd

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed
+41-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,49 @@
11
"""
22
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.
47
5-
Time Complexity:
6-
-
8+
Time Complexity: O(s * t)
9+
- for ๋ฃจํ”„๋กœ s์˜ ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณต
10+
- while ๋ฃจํ”„ ์•ˆ์˜ all() ์กฐ๊ฑด์—์„œ t์˜ ๊ฐ ๋ฌธ์ž ๋น„๊ต
11+
- ๋”ฐ๋ผ์„œ O(s * t)
712
8-
Space Complexity:
9-
-
13+
Space Complexity: O(s + t)
14+
- t_counts: O(t)์˜ ๊ณต๊ฐ„
15+
- w_counts: O(s)์˜ ๊ณต๊ฐ„
16+
- ๋”ฐ๋ผ์„œ O(s + t)
1017
1118
ํ’€์ด๋ฐฉ๋ฒ•:
12-
1.
19+
1. Counter๋กœ t์˜ ๋ฌธ์ž ๋นˆ๋„์ˆ˜ ์ €์žฅ
20+
2. ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ๋กœ s ํƒ์ƒ‰:
21+
- high ํฌ์ธํ„ฐ๋กœ ์œˆ๋„์šฐ๋ฅผ ํ™•์žฅํ•˜๋ฉฐ ํ•„์š”ํ•œ ๋ฌธ์ž ์ฐพ๊ธฐ
22+
- ํ•„์š”ํ•œ ๋ฌธ์ž๋ฅผ ๋ชจ๋‘ ์ฐพ์œผ๋ฉด low ํฌ์ธํ„ฐ๋กœ ์œˆ๋„์šฐ ์ถ•์†Œ
23+
3. ์ตœ์†Œ ๊ธธ์ด์˜ ์œˆ๋„์šฐ ๋ฐ˜ํ™˜
24+
25+
๋ฉ”๋ชจ:
26+
- ํ’€์ด ๋ฐฉ๋ฒ•์„ ๋ณด๊ณ  ์ตํž˜
27+
- ํ•ด์‹œ๋งต๊ณผ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ๊ด€๋ จ ๋‹ค๋ฅธ ๋ฌธ์ œ๋“ค์„ ํ’€๊ณ  ์ด ๋ฌธ์ œ ์Šค์Šค๋กœ ํ’€์–ด๋ณด๊ธฐ
1328
"""
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 ""
1449

0 commit comments

Comments
ย (0)