readable sliding window python solution with dictionary O(n) according to Joshua924


  • 1
    K

    a readable python solution according to the
    p0 is the start point,maxs is our solution, maxcount record the most frequency character.

            dic = {}
            p0,maxs,maxcount = 0,0,0
            for p1 in xrange (len(s)):
                dic[s[p1]] = dic.get(s[p1],0) + 1
                if maxcount < dic[s[p1]]:
                    maxcount = dic[s[p1]]
                while p1 - p0 - maxcount + 1 > k:
                    dic[s[p0]] -= 1
                    p0 += 1
                maxs = max(maxs,p1-p0+1)
            return maxs
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.