Ask for the longest string, requiring all the letters in the string not to repeat the idea
Set head and tail two pointers, tail each move forward one cell, check whether all letters between tail and head are duplicated with the letter tail pointing, and if you repeat, point head to the next
For example:
ABCDEFDC, the current head points to A,tail to F, and when tail points to the next D, scans the value between head and tail, finds that D and tail that follow C are repeating, and then adjusts the head to point to E.
class Solution {
public:
int lengthOfLongestSubstring(string s) {
if (s == "")
return 0;
if (s.size() == 1)
{
return 1;
}
int head = 0, tail = 1, length = 1;;
for (; tail < s.size(); tail++)
{
for (size_t j = head; j < tail; j++)
{
if (s[j] == s[tail])
{
head = j + 1;
break;
}
}
length = (tail - head+1)>length ? (tail - head+1) : length;
}
return length;
}
};
From for notes (Wiz)
Longest Substring without repeating characters