Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int longestSubstringKDistinct(string str, int k){
- int start = 0, res = 0;
- unordered_map<char, int> um;
- for (int i = 0; i < str.size(); ++i)
- {
- um[str[i]]++;
- while(um.size() > k){
- char left = str[start];
- if(--um[left] == 0)
- um.erase(left);
- start++;
- }
- res = max(res, i - start + 1); //no need to put in while block as if the size is k, the res will be updated
- }
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement