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.
class Solution {
public:
/*algorithm: two pointer
*/
int lengthOfLongestSubstring(string s) {// "abcabcbb"
vector<int>table(256,-1);
int b = 0,i = b;
int maxLen = 0;
while(i < s.size()){
if(table[s[i]] >=0)//repeat
{
maxLen = max(maxLen,i-b);
for(int k=b;k <table[s[i]];k++)table[s[k]]=-1;
b = table[s[i]]+1;
table[s[i]] = i;
}else{
table[s[i]] =i;
}
++i;
}
maxLen = max(maxLen,i-b);//for end of string case
return maxLen;
}
};