Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import defaultdict
- class Solution:
- def lengthOfLongestSubstringKDistinct(self, s: 'str', k: 'int') -> 'int':
- n = len(s)
- if k == 0 or n == 0:
- return 0
- # sliding window left and right pointers
- left, right = 0, 0
- # hashmap character -> its rightmost position
- # in the sliding window
- hashmap = defaultdict()
- max_len = 1
- while right < n:
- # add new character and move right pointer
- hashmap[s[right]] = right
- right += 1
- # slidewindow contains 3 characters
- if len(hashmap) == k + 1:
- # delete the leftmost character
- del_idx = min(hashmap.values())
- del hashmap[s[del_idx]]
- # move left pointer of the slidewindow
- left = del_idx + 1
- max_len = max(max_len, right - left)
- return max_len
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement