fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. long long findLargestValidSubstring(const string &s, long long k) {
  5. set<char> g;
  6. long long n = s.length();
  7. long long i = 0, j = 0;
  8. long long p = 0;
  9.  
  10. while (j < n) {
  11. g.insert(s[j]);
  12. char max_char = *(g.rbegin());
  13. char min_char = *(g.begin());
  14.  
  15. while ((max_char - min_char) > k) {
  16. g.erase(s[i]);
  17. i++;
  18. max_char = *(g.rbegin());
  19. min_char = *(g.begin());
  20. }
  21.  
  22. p = max(p, j - i + 1);
  23. j++;
  24. }
  25.  
  26. return p;
  27. }
  28.  
  29. int main() {
  30. long long k;
  31. string s;
  32. cin >> k;
  33. cin >> s;
  34.  
  35. cout << findLargestValidSubstring(s, k) << "\n";
  36.  
  37. return 0;
  38. }
  39.  
Success #stdin #stdout 0.01s 5280KB
stdin
2
zxyzyz
stdout
6