[Leet Code 3] Longest Substring without repeating characters

Source: Internet
Author: User

1 topics

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.

Hide TagsHash Table Pointers String2 thinking began to think is to get all the non-repetition of the line, and later found to be continuous substring. Then think of every time you encounter duplicates, from that point the next one begins to re-add.  Later, a bug test input was encountered and time timed out. See other people's ideas, using the topic hint of the pointers. Also, the table's key,value settings are fastidious, key is the letter, and value is the position. The key I started thinking about was a different location. Original link: Https://leetcode.com/discuss/17939/my-accepted-o-n-java-solutionok, see Code 3 code
        Public intlengthoflongestsubstring (String s) {Hashtable<Character,Integer> hash=NewHashtable<>(); intLength=s.length (); intMax=0; intAvailablefrom=0;  for(inti=0;i<length;i++){               if(Hash.containskey (S.charat (i))) {//int last = The largest index where have the same character (before current index i)                   intlast=(Integer) hash.get (S.charat (i)); //int available-from = The next index from where latest duplication ends (before current index i) //The biggest, prevent ABBA, traverse to the second A, last will be smaller than AvailableformAvailablefrom=math.max (AvailableFrom, last+1); }             //Then the possible substring is located between Available-from and I. Max=math.max (Max, i-availablefrom+1);                   Hash.put (S.charat (i), i); }           returnMax; }

[Leet Code 3] Longest Substring without repeating characters

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.