Skip to content

Commit 228bb32

Browse files
committed
feat: Add Solution to DaleStudy#243
1 parent 1d77eeb commit 228bb32

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution:
2+
def characterReplacement(self, s: str, k: int) -> int:
3+
# Time Complexity: O(n)
4+
# Space Complexity: O(1)
5+
n = len(s)
6+
if n == 0:
7+
return 0
8+
9+
left = 0
10+
max_len = 0
11+
char_counts = {}
12+
max_freq_count = 0
13+
14+
for right in range(n):
15+
right_char = s[right]
16+
char_counts[right_char] = char_counts.get(right_char, 0) + 1
17+
max_freq_count = max(max_freq_count, char_counts[right_char])
18+
19+
current_window_length = right - left + 1
20+
# ๋ฐ”๊ฟ”์•ผ ํ•˜๋Š” ๋ฌธ์ž ์ˆ˜ = ์œˆ๋„์šฐ ๊ธธ์ด - ๊ฐ€์žฅ ๋งŽ์€ ๋ฌธ์ž์˜ ๋นˆ๋„์ˆ˜
21+
changes_needed = current_window_length - max_freq_count
22+
23+
# ๋งŒ์•ฝ ๋ฐ”๊ฟ”์•ผ ํ•˜๋Š” ๋ฌธ์ž ์ˆ˜๊ฐ€ k๋ณด๋‹ค ํฌ๋ฉด ์œˆ๋„์šฐ๋ฅผ ์ค„์—ฌ์•ผ ํ•จ
24+
if changes_needed > k:
25+
left_char = s[left]
26+
char_counts[left_char] -= 1
27+
if char_counts[left_char] == 0:
28+
del char_counts[left_char]
29+
left += 1
30+
31+
max_len = max(max_len, right - left + 1)
32+
33+
return max_len

0 commit comments

Comments
ย (0)