fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. #include <unordered_set>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. string s = "AllPossibleSubstrings";
  11. int l=0,index=0;
  12. for(int i =0;i<s.length();i++)
  13. {
  14. for(int j=i+1;j<s.length();j++)
  15. {
  16. string sub = string(s.begin()+i,s.begin()+j);
  17. unordered_set<char> v;
  18. for(auto x:sub)
  19. {
  20. v.insert(x);
  21. }
  22. if(v.size()<=2) {
  23. l=max(l,j-i+1);
  24. if(l==j-i+1) {
  25. index=i;
  26. cout << "Found match " << i << " " << j << " " << l << " " << sub << endl;
  27. }
  28. }
  29. }
  30. }
  31.  
  32. cout<<l<<" "+s.substr(index,l)<<endl;
  33.  
  34. }
Success #stdin #stdout 0s 3276KB
stdin
Standard input is empty
stdout
Found match 0 1 2 A
Found match 0 2 3 Al
Found match 0 3 4 All
Found match 1 4 4 llP
Found match 4 7 4 oss
Found match 5 8 4 ssi
4 ssib