Sum
Input:
s = "eceba", k = 2
Output:
3
Explanation:
T is "ece" which its length is 3.Input: s = "aa", k = 1
Output:
2
Explanation:
T is "aa" which its length is 2.Last updated
Was this helpful?
Input:
s = "eceba", k = 2
Output:
3
Explanation:
T is "ece" which its length is 3.Input: s = "aa", k = 1
Output:
2
Explanation:
T is "aa" which its length is 2.Last updated
Was this helpful?
Was this helpful?
class Solution(object):
def lengthOfLongestSubstringKDistinct(self, s, k):
"""
:type s: str
:type k: int
:rtype: int
"""
d, low, ans = {}, 0, 0
for i,c in enumerate(s) :
d[c] = i
if len(d) > k:
low = min(d.values())
del d[s[low]]
low += 1
ans = max(i - low + 1, ans)
return ans