Longest Repeating Character Replacement
https://leetcode.com/problems/longest-repeating-character-replacement/
Solution in O(N)
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
index = defaultdict(int)
start = 0
end = 0
res = 0
cur_max = 0
for end in range(len(s)):
index[s[end]] += 1
cur_max = max(cur_max, index[s[end]])
while (end - start + 1) - cur_max > k:
index[s[start]] -= 1
start += 1
res = max(res, end - start + 1)
return resSolution for replacing multiple characters
Last updated
Was this helpful?