典型的滑动窗口双指针技巧
class Solution {
public:
int lengthOfLongestSubstringKDistinct(string s, int k) {
int n = s.size(), res = 0;
unordered_map<char,int> hashmap;
for(int i=0,j=0;j<n;j++){
hashmap[s[j]]++;
while(i<n&&hashmap.size()>k){
hashmap[s[i]]--;
if(hashmap.count(s[i])&&hashmap[s[i]]==0) hashmap.erase(s[i]);
i++;
}
res = max(res,j-i+1);
}
return res;
}
};