Leetcode 3 longest Substring without repeating characters

Source: Internet
Author: User

1. Topic Requirements

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.

2. Analysis

After reading the topic, the first solution that appears in my mind is to traverse the string s, and when a character is obsolete in a string S1 than its predecessor, it is handled as follows:

Assuming the initial string s, two subscript i,j, scan from the beginning, when scanning to j+1, s[j+1] belongs to S[0......J], note that the characters in S[0......J] are not the same. Suppose s[i]=s[j+1],result = j-0+1; Next, if there is a substring that satisfies the condition and is larger than result, it must be a substring starting from s[i+1].

Because s[i]=s[j+1], therefore, any substring that begins with a s[1......i] character, and the string that satisfies the question is terminated with s[j], its length must be less than result. If you want to find a substring that is longer than the result and satisfies the requirements, you must start looking backwards from s[i+1].

The code is as follows:

classSolution { Public:    intLengthoflongestsubstring (strings) {intres=0, i=0, j=0; intLength =s.length (); if(0==length) {            return 0; }        Set<Char>MySet;        Myset.clear ();  while(j<length) {            if(Myset.find (s[j]) = =Myset.end ())                {Myset.insert (s[j]); J++; }            Else            {                if(J-i > Res) res = ji;                Myset.clear (); I++; J=i; }        }        if(J-i > Res) res = ji; returnRes; }};

Submit an answer. Time Limit exceeded. Oh, sad.

Re-judging the algorithm, we will find that the algorithm will produce backtracking, that is, J is assigned to I, resulting in backtracking, the algorithm time complexity is the worst case is O (2n). So we need to find a way to avoid this extra overhead and make sure J never goes back in the direction.

In the above method, when the discovery of s[i]=s[j+1], we are to take the subscript J back move, we can change an angle, the start will be moved back to the i+1 place, it is important to note that when start moves, it is necessary to remove S[start] from the set. This algorithm needs to preserve the subscript of the characters that appear before

The code is as follows:

1 classSolution {2  Public:3     intLengthoflongestsubstring (strings) {4         intres=0;5         intLength =s.length ();6         if(0==length)7         {8             return 0;9         }Ten         intI=0, j=1; One         intindex[ the]; Amemset (index,-1,sizeof(index)); -index[s[0]]=0; -          while(i<j&&j<length)//"ABCABCBB" the         { -             if(-1==Index[s[j]]) -             { -index[s[j]]=J;  +  -             } +             Else A             { at                 if(j-i>res) res = ji; -                  while(true) -                 { -                     if(s[i]!=S[j]) -                     { -index[s[i]]=-1; ini++;  -}Else to                          Break; +                      -                 } thei++; *             } $J + +;Panax Notoginseng  -  the         } +         if(j-i>res) res = ji; A         returnRes; the     } +};

Submit code, Accepted.

Leetcode 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.