Topic Three: 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 are "B", with the length of 1.
Main topic:
Give a string s to find out the length of the longest and not repeating string in the string. For example: "ABCABCBB", the longest and no repetition of the string is "abc", then should return 3
Solution to the idea:
1. Save the location of each character by creating a dictionary
2. To find the length of the current non-repeating string, compare the maximum
1 intLengthoflongestsubstring (strings) {2 if(s.size () = =0)3 return 0;4map<int,int>dict;5 6 intMaxLen =0;7 intLen =0;8 9 for(inti =0; I < s.size (); i++) {Tenlen++; One //if it is a repeating character and is in the current string, the length of the current string (not including the character) is computed A if(Dict.count (s[i]) && len > (i-Dict[s[i])) -Len = i-Dict[s[i]]; -MaxLen =Max (MaxLen, Len); theDict[s[i]] =i; - } - returnMaxLen; -}
Longest Substring without repeating characters