Skip to content

Commit 83ce8a4

Browse files
committed
longest-repeating-character-replacement sol (py)
1 parent b786384 commit 83ce8a4

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

longest-repeating-character-replacement/hi-rachel.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,25 @@ def characterReplacement(self, s: str, k: int) -> int:
4444
max_len = max(max_len, right - left + 1)
4545

4646
return max_len
47+
48+
49+
from collections import Counter
50+
51+
class Solution:
52+
def characterReplacement(self, s: str, k: int) -> int:
53+
count = Counter()
54+
l = 0
55+
max_freq = 0
56+
max_length = 0
57+
58+
for r, ch in enumerate(s):
59+
count[ch] += 1
60+
max_freq = max(max_freq, count[ch])
61+
62+
if (r - l + 1) - max_freq > k:
63+
count[s[l]] -= 1
64+
l += 1
65+
66+
max_length = max(max_length, r - l + 1)
67+
68+
return max_length

0 commit comments

Comments
 (0)