fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int lengthOfLongestSubstring(string s) {
  4. int i = 0, j=1;
  5. int ans = 1;
  6. map<char, int>mp;
  7. mp.insert({s[0], 0});
  8. while(j <= s.length()){
  9. auto itr = mp.find(s[j]);
  10. if(itr != mp.end()){
  11. ans = max(ans, j - i);
  12. i = mp[s[j]] + 1;
  13. mp[s[j]] = j;
  14. }
  15. else
  16. mp.insert({s[j], j});
  17. cout<<i<<" "<<j<<" "<<s[j]<<" "<<ans<<endl;
  18. j++;
  19. }
  20. ans = max(ans, j - i);
  21. // cout<<ans;
  22. return ans;
  23. }
  24. int main(){
  25. string s;
  26. cin >> s;
  27. int ans = lengthOfLongestSubstring(s);
  28. cout<<ans;
  29. }
Success #stdin #stdout 0s 4400KB
stdin
abba
stdout
0 1 b 1
2 2 b 2
1 3 a 2
1 4  2
4