Time Complexity: O(N)
| class Solution { | |
| public: | |
| int lengthOfLongestSubstring(string s) { | |
| vector< int >cnt(256,0); | |
| int mx = 0; | |
| int st = 0; | |
| for(int i=0; i<(int)s.size(); i++){ | |
| cnt[s[i]]++; | |
| if(cnt[ s[i] ] > 1){ | |
| for(int j=st; j<i; j++){ | |
| cnt[ s[j] ]--; | |
| if( s[i] == s[j] ){ | |
| st = j+1; | |
| break; | |
| } | |
| } | |
| } | |
| mx = max(mx, i - st + 1); | |
| } | |
| return mx; | |
| } | |
| }; |