Problem: Longest substring with at most distinct characters.
function lengthOfLongestSubstringKDistinct(s, k):
charCount = empty map
left = 0, maxLen = 0
for right from 0 to s.length - 1:
charCount[s[right]]++
while charCount.size > k:
charCount[s[left]]--
if charCount[s[left]] == 0:
charCount.remove(s[left])
left++
maxLen = max(maxLen, right - left + 1)
return maxLen
Time: . Space: .