Python Sliding Window


  • 0
    class Solution(object):
        def lengthOfLongestSubstringKDistinct(self, s, k):
            left, right, dic, ans = 0, 0, {}, 0
            while right < len(s):
                dic[s[right]] = dic.get(s[right], 0) + 1
                right += 1
                while len(dic) > k:
                    dic[s[left]] -= 1
                    if dic[s[left]] == 0:
                        del dic[s[left]]
                    left += 1
                ans = max(ans, right - left)
            return ans
    

Log in to reply
 

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