Skip to content

Latest commit

 

History

History
37 lines (27 loc) · 727 Bytes

424.md

File metadata and controls

37 lines (27 loc) · 727 Bytes

424 Longest Repeating Character Replacement

Description

link


Solution

传统滑动窗口可以解决问题,看代码


Code

O(n)

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

        cnt = collections.Counter(s[l])
        while r < len(s) - 1:
            r += 1
            cnt[s[r]] += 1
            max_cnt_num = cnt.most_common(1)[0][1]
            while r - l + 1 - max_cnt_num > k:
                cnt[s[l]] -= 1
                l += 1
            res = max(res, r - l + 1)
        return res