Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.47 KB | None | 0 0
  1. int longestSubstringKDistinct(string str, int k){
  2. int start = 0, res = 0;
  3. unordered_map<char, int> um;
  4. for (int i = 0; i < str.size(); ++i)
  5. {
  6. um[str[i]]++;
  7. while(um.size() > k){
  8. char left = str[start];
  9. if(--um[left] == 0)
  10. um.erase(left);
  11. start++;
  12. }
  13. res = max(res, i - start + 1); //no need to put in while block as if the size is k, the res will be updated
  14. }
  15. return res;
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement