1 class Solution { 2 public: 3 int lengthOfLongestSubstring(string s) { 4 int n = s.length(); 5 int i = 0, j = 0; 6 int maxLen = 0; 7 bool exist[256] = { false }; 8 while (j < n) { 9 if (exist[s[j]]) { 10 maxLen = max(maxLen, j-i); 11 while (s[i] != s[j]){ 12 exist[s[i]] = false; 13 i++; 14 } 15 i++; 16 j++; 17 } else { 18 exist[s[j]] = true; 19 j++; 20 } 21 } 22 maxLen = max(maxLen, n-i); 23 return maxLen; 24 } 25 };