fork download
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. int qs8_google(vector<char>arr,int k){
  5. int n=arr.size();
  6. multiset<char>st;
  7. int max_len=0;
  8. for(int i=0,j=0;j<n;j++){
  9. st.insert(arr[j]);
  10. int diff=*st.rbegin()-*st.begin();
  11. while(diff>k){
  12. auto it=st.find(arr[i]);
  13. st.erase(it);
  14. i++;
  15. diff=*st.rbegin()-*st.begin();
  16. }
  17. int len=j-i+1;
  18. max_len=max(max_len,len);
  19. }
  20. return max_len;
  21. }
  22.  
  23. int main(){
  24. int k;
  25. string st;
  26. getline(cin,st);
  27. int l=st.size();
  28. vector<char>arr(l);
  29. for(int i=0;i<l;i++){
  30. arr[i]=st[i];
  31. }
  32. cin>>k;
  33. int ans=qs8_google(arr,k);
  34. cout<<"Length of the longest substring with abs diff between any pair of the chars within the substr is: "<<ans<<endl;
  35.  
  36. }
Success #stdin #stdout 0.01s 5292KB
stdin
dgzdgabbab
2
stdout
Length of the longest substring with abs diff between any pair of the chars within the substr is: 5