Skip to content

Latest commit

 

History

History
23 lines (21 loc) · 607 Bytes

424.md

File metadata and controls

23 lines (21 loc) · 607 Bytes

424. Longest Repeating Character Replacement

Solution 1 (time O(n), space O(1))

class Solution(object):
    def characterReplacement(self, s, k):
        """
        :type s: str
        :type k: int
        :rtype: int
        """
        p1, cnt = 0, [0] * 26
        ans = 0
        for p2 in range(len(s)):
            cnt[ord(s[p2]) - ord('A')] += 1
            while sum(cnt) - max(cnt) > k and p1 < p2:
                cnt[ord(s[p1]) - ord('A')] -= 1
                p1 += 1
            if sum(cnt) - max(cnt) <= k:
                ans = max(ans, p2 - p1 + 1)
        return ans