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.
The O (n) solution is obtained by using map to record key information:
Class solution {public: int lengthoflongestsubstring (string s) {If (S. size () <2) return S. size (); int lengthofcurrent = 1, maxlength = 1; // lengthofcurrent indicates the number of unique characters before the current character. Map <char, int> m; // subscript of characters and characters in S stored in M. M [s [0] = 0; For (INT I = 1; I <S. size (); I ++) {If (M. count (s [I]) = 0 | M [s [I] <I-lengthofcurrent) // The current character is not repeated or repeated, but before the previous character is repeated, the current maximum length of lengthofcurrent ++ is not affected; else lengthofcurrent = I-m [s [I]; m [s [I] = I; If (lengthofcurrent> maxlength) maxlength = lengthofcurrent;} return maxlength ;}};
[Leetcode] Longest substring without repeating characters