Skip to content

Commit 0320536

Browse files
committed
solve longest repeating character replacement
1 parent 95a781c commit 0320536

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# TC : O(n)
2+
# SC : O(1)
3+
class Solution:
4+
def characterReplacement(self, s: str, k: int) -> int:
5+
count = {}
6+
result = 0
7+
8+
left = 0
9+
maxFrequency = 0
10+
for right in range(len(s)):
11+
count[s[right]] = 1 + count.get(s[right], 0)
12+
maxFrequency = max(maxFrequency, count[s[right]])
13+
14+
while (right - left + 1) - maxFrequency > k:
15+
count[s[left]] -= 1
16+
left += 1
17+
18+
result = max(result, right - left + 1)
19+
20+
return result

0 commit comments

Comments
 (0)