Skip to content

Commit 72ea223

Browse files
committed
solve 2
1 parent 76ac42c commit 72ea223

File tree

1 file changed

+26
-0
lines changed
  • longest-repeating-character-replacement

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'''
2+
μ‹œκ°„λ³΅μž‘λ„: O(n)
3+
- λ¬Έμžμ—΄μ˜ 길이만큼 ν•œ 번만 μˆœνšŒν•©λ‹ˆλ‹€.
4+
κ³΅κ°„λ³΅μž‘λ„: O(n)
5+
- char_count λ”•μ…”λ„ˆλ¦¬λŠ” μ΅œλŒ€ μ•ŒνŒŒλ²³ 26개만 μ €μž₯ν•©λ‹ˆλ‹€.
6+
'''
7+
8+
class Solution:
9+
def characterReplacement(self, s: str, k: int) -> int:
10+
left = 0
11+
max_count = 0
12+
max_length = 0
13+
char_count = {}
14+
15+
for right in range(len(s)):
16+
char_count[s[right]] = char_count.get(s[right], 0) + 1
17+
max_count = max(max_count, char_count[s[right]])
18+
19+
# If the remaining characters exceed the allowed k changes
20+
while (right - left + 1) - max_count > k:
21+
char_count[s[left]] -= 1
22+
left += 1
23+
24+
max_length = max(max_length, right - left + 1)
25+
26+
return max_length

0 commit comments

Comments
Β (0)