Leetcode 424 Solution

This article provides solution to leetcode question 424 (longest-repeating-character-replacement)

https://leetcode.com/problems/longest-repeating-character-replacement

Solution

class Solution:
    def characterReplacement(self, s: str, k: int) -> int:
        l = 0
        r = 0
        opt = 0
        best = 0

        m = collections.defaultdict(int)

        while r < len(s):
            m[s[r]] += 1

            best = max(best, m[s[r]])

            while r - l + 1 > best + k:
                m[s[l]] -= 1
                l += 1

            opt = max(opt, r - l + 1)
            r += 1

        return opt