fork download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6.  
  7. int main() {
  8. ios_base::sync_with_stdio(false);
  9. cin.tie(NULL);
  10.  
  11. ll n, k;
  12. cin>>n>>k;
  13.  
  14. string s;
  15. cin>>s;
  16.  
  17. multiset<char> g;
  18. ll p = 0;
  19.  
  20. for(int i = 0,j=0; j < n; j++){
  21. g.insert(s[j]);
  22.  
  23. ll diff = *g.rbegin() - *g.begin();
  24.  
  25. while(diff > k){
  26. g.erase(g.find(s[i]));
  27. i++;
  28. if(!g.empty()){
  29. diff = *g.rbegin() - *g.begin();
  30. }
  31. }
  32. ll length = j - i + 1;
  33. p = max(p, length);
  34. }
  35. cout<<p<<endl;
  36. return 0;
  37. }
Success #stdin #stdout 0.01s 5316KB
stdin
10 2 
dgzdgabbab
stdout
5