Longest Repeating Character Replacement

Problem Id: 424 Difficulty: Medium


Intuition

Solution


class Solution:
    def characterReplacement(self, s: str, k: int) -> int:
        if not s:
            return 0

        counts = [0] * 26
        lo = 0
        hi = 0
        longest = 0
        while hi < len(s):
            counts[ord(s[hi]) - 65] += 1
            hi += 1
            while hi - lo - max(counts) > k:
                counts[ord(s[lo]) - 65] -= 1
                lo += 1
            longest = max(longest, hi - lo)
        return longest