Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #User function Template for python3
- class Solution:
- def longestKSubstr(self, s, k):
- d={}
- n=len(s)
- l=0
- ans=0
- for r in range(n):
- while len(d)>k:
- if d[s[l]]==1:
- d.pop(s[l])
- else:
- d[s[l]]-=1
- l+=1
- d[s[r]]=1+d.get(s[r],0)
- if len(d)==k:
- ans=max(ans,r-l+1)
- return ans if ans else -1
Add Comment
Please, Sign In to add comment