title : input is a string that finds the length of the oldest string without repeating characters
Example :
" ABCABCBB " the eldest string ( ABC ) Length is 3
" bbbbbbb " the eldest string ( b ) Length is 1
"Abdevbac" eldest string (bdev) Length 4
algorithm Idea :
Set two subscript identifiers, initially at the head of the array, and set a hashset. Identify runner to go backward, and put the passed characters into the hashset, when there is duplicate character, stop moving, at this time, the logo walker chasing after, until Walker's character and runner characters are the same so far, At this point the character between Walker and runner is a string with no duplicates, and the length is recorded as Max. The length value of the longest string can be obtained after a traversal.
Complexity of Time :
Two identities need to be traversed once, i.e. 2*n, so the complexity is: O (N)
Code Implementation :
public int lengthoflongestsubstring (String s) {
if (S==null | | s.length () ==0)
return 0;
hashset<character> set = new hashset<character> ();
int max = 0;
int walker = 0;
int runner = 0;
while (Runner<s.length ())
{
if (Set.contains (S.charat (runner)))
{
while (S.charat (Walker)!=s.charat (runner))
{
Set.remove (S.charat (Walker));
walker++;
}
if (Max<runner-walker)
{
max = Runner-walker;
}
walker++;
}
Else
{
Set.add (S.charat (runner));
}
runner++;
}
max = Math.max (Max,runner-walker);
return Max;
}
"Algorithm" finds the length of the oldest string without repetition