1. 程式人生 > >python leetcode 424. Longest Repeating Character Replacement

python leetcode 424. Longest Repeating Character Replacement

知道題目考察的是什麼非常重要 統計連續子串中最大重複字元(允許修改k次)應該能想到滑動視窗

class Solution:
    def characterReplacement(self, s, k):
        """
        :type s: str
        :type k: int
        :rtype: int
        """
        start=end=res=maxcur=0
        count=[0]*26
        while end<len(s):
            count[ord(s[end])-65]+=1
            maxcur=max(maxcur,max(count))
            while (end-start+1-maxcur)>k:
                  count[ord(s[start])-65]-=1
                  start+=1
            res=max(res,end-start+1)
            end+=1
        return res