Leetcode "3". Longest Substring without repeating characters--algorithm diagram and Java implementation

Source: Internet
Author: User
Tags repetition

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          { &nbsp ;           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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.