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.
int lengthOfLongestSubstring(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int longest = 0;
int slen = s.length();
if(slen < 0) return 0;
if(slen == 1) return 1;
int pre = 1;
for(int i = 1; i < slen; ++i)
{
char tmp = s[i];
int j = i-1;
int count = 1;
while((j>=0) && (tmp != s[j]))
{j--;count++;}
if(count >= pre+1) count = pre+1;
if(count > longest) longest = count;
pre = count;
}
return longest;
}