The eldest son with no repeat character in English description
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "ABCABCBB", the answer is "ABC" and which the length is 3.
Given "BBBBB", the answer is ' B ', with the length of 1.
Given "Pwwkew", the answer is ' Wke ', with the length of 3. Note that the answer must to be a substring, "Pwke" are a subsequence and not a substring. Chinese description
Given a string, find the length of the oldest string that does not contain duplicate characters.
Example:
Given "ABCABCBB", the oldest string without repeated characters is "abc", then the length is 3.
Given "BBBBB", the longest substring is "B", the length is 1.
Given "Pwwkew", the eldest son string is "Wke", and the length is 3. Note that the answer must be a substring, and "Pwke" is a subsequence rather than a substring.
Algorithm reference: https://www.nowcoder.com/questionTerminal/5947ddcc17cb4f09909efa7342780048
"Sliding window Solution"
For example ABCABCCC when you scan the right to the ABCA you have to delete the first A to get BCA,
and then the "window" continues to slide to the right, and whenever a new char is added, the left side checks for duplicate char,
Then if there is no repetition of the normal add,
there are repeated words on the left to throw away part (from the leftmost to repeat char this paragraph thrown away), in this process to record the maximum window length
By code:
int lengthoflongestsubstring (string inputstring) {Unordered_map<char, int> charmap = {};
int maxLength = 0;
int leftpos = 0;
for (Auto i = 0; i < inputstring.size (); i++) {char Curchar = inputstring[i];//current character if (Charmap.find (Curchar)!= charmap.end ()) {//The current character has already appeared, find its position int Existchar
Pos = Charmap[curchar];
LeftPos = Max (LeftPos, Existcharpos + 1);
} maxLength = max (maxLength, I-leftpos + 1);
Charmap[curchar] = i;
return maxLength; }