Third question longest Substring without repeating characters as follows:
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.
Picture one, the subject window scan
thought: (Please forgive me if the painting is not good). Maintaining a window [Walker,runner]:
first , the window interval upper limit runner runs in front, each scan, adds the corresponding element to the HashSet, until scans to hashset already existing element, runner stops, is recorded as Runner1. Wait a little slower walker. At this time, the same element exists in the window as Walker;
two , first record walker and runner distance, because at this time Walker forward substring is no element repetition, and max size. Immediately thereafter, Walker could not lag behind and walk up to the same place as the runner elements. While walking and throwing away those elements from the hashset, go straight to find the same place as the runner1 element, and remember it as walker1. The reason for Walker1 in the front part is that it is impossible to find a larger interval containing the [walker1,runner1] interval so that there is no repetition, because there are already repeating elements in the [Walker1,runner1] interval. So the next target range can only go forward, the lower interval is lost, at this point to the third step;
three , the Walker1 front (including itself) lost, the problem back to the first step, repeat the first step of the process can be.
The Java code is as follows;
public class Solution { public int lengthoflongestsubstring (String s) { if (s==null && Amp S.length () ==0) return 0; hashset<character> set = new hashset<character> (); int max = 0; int walker = 0; int runner = 0; for (; Runner<s.length (); runner++) { if ( Set.contains (S.charat (runner))) { max = (Runner-walker) >max? (runner-walker):max; while (S.char At (Walker)!=s.charat (runner) { and nbsp Set.remove (S.charat (Walker)); walker++; } walker++ ; } Else {   ; Set.add (S.charat (runner)); } } max = (runner-walker) >max? (runner-walker): Max; return Max; } }
Learning Reference:
[1]. Leetcode–longest Substring without repeating characters (Java)
[2]. repeating characters--leetcode
Leetcode "3". Longest Substring without repeating characters--algorithm diagram and Java implementation