Title Description: 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" are "abc", which the length is 3. For "bbbbb" the longest substring are "B", with the length of 1.
Precautions:
1. Are all characters, or a A-Z letter?
2. After finding the same character, how do I handle the next character? If the same character is found in places I and J, then the starting position of the next string should be i+1 (assuming I < j);
3. Time complexity is O (n).
The code is as follows:
classSolution { Public: intLengthoflongestsubstring (strings) {Const intAscii_max = the; intCharmap[ascii_max];//record where the character last occurred intStart =0;//record the starting position of the current substring intMax_len =0; Fill (charmap, Charmap+ Ascii_max,-1);//Fill AS-1 for(inti =0; I < s.size (); i++){ if(Charmap[s.at (i)] >=start) {Max_len= Max (I-start, Max_len); Start= charmap[s.at (i)] +1; } charmap[s.at (i)]=i; } returnMax (int) s.size ()-Start, Max_len);//including all the different situations }};
Leetcode 003 Longest Substring without repeating characters