Advertisement
a53

DistinctLetters

a53
Jun 21st, 2021
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. int DistLetters(string s, int k)
  2. {
  3. int longest = 0, start = 0, distinct_count = 0;
  4. array<int, 256> visited = {0};
  5. for (int i = 0; i < s.length(); ++i)
  6. {
  7. if (visited[s[i]] == 0)
  8. ++distinct_count;
  9. ++visited[s[i]];
  10. while(distinct_count > k)
  11. {
  12. --visited[s[start]];
  13. if (visited[s[start]] == 0)
  14. {
  15. --distinct_count;
  16. }
  17. ++start;
  18. }
  19. longest += i - start + 1;
  20. }
  21. return longest;
  22.  
  23. }
  24. int DistinctLetters(string s, int L, int U)
  25. {
  26.  
  27. return (DistLetters(s,U)-DistLetters(s,L-1));
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement