Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3.
For "bbbbb" the longest substring is "b", with the length of 1.
1 class Solution { 2 public: 3 int lengthOfLongestSubstring(string s) { 4 vector<bool> exist(256, false); 5 int res = 0; 6 int start = 0, end = 0; 7 int N = s.size(); 8 while(end < N) { 9 if(!exist[s[end]]) { 10 exist[s[end++]] = true; 11 } 12 else { 13 exist[s[start++]] = false; 14 } 15 res = max(end-start, res); // already +1 in var: end 16 } 17 return res; 18 } 19 };