Leetcode:3_longest Substring without repeating characters | The length of the oldest string without repeating characters | Medium

Source: Internet
Author: User

Topic:

 Given a string , find the length of the longest substring without repeating chara Cters. For example, the longest substring without repeating letters for   " ABCABCBB  "  is   abc  , which the length is  3 . For  BBBBB   the longest substring is   " b  " , with the length of 1 . 

Problem Solving Ideas:

This problem lets you find the length of the oldest string with a non-repeating word, such as: ABABC, a substring of ABC, and a length of 3. There are several ways to do this:

Method One:

Rely on some of the unique functions of the string itself to do the corresponding operation. We can maintain a substring to hold the longest non-repeating substring and record the length of the current substring, and if repeated characters are encountered, the repeated characters in the substring are removed, and the longest non-repeating substring can eventually be found. such as str = ABABC, substr = A, AB, BA, AB, abc .... Like this way of thinking. The following code:

//method One: string onlyintLengthoflongestsubstring (strings) {size_t J=1; if(S.size () <=1)        returns.size (); intLen =1, Nmaxlen =0; stringsubStr; Substr.push_back (s[0]);  while(J <s.size ()) {        if(Substr.find (s[j]) = =string:: NPOs)        {Substr.push_back (s[j]); }        Else {            if(Len >Nmaxlen) Nmaxlen=Len;  while(Substr.find (s[j])! =string:: NPOs) {Substr.erase (0,1); Len--;        } substr.push_back (S[j]); } Len++; J++; }    if(Len >Nmaxlen) Nmaxlen=Len; returnNmaxlen;}

Method Two:

Pointer method: Use a pointer to the left edge of the string, if you encounter duplicate characters, move backwards, and use a 26-bit character array (because a total of 26 characters) to save the position of the last occurrence of each character, so as to update the distance between the pointer position and the character position, You can figure out the length of the longest non-repeating character, as shown in the following code:

1 //method Two: pointer2 intLengthOfLongestSubstring2 (strings) {3     intMaxLen =0, left =0;4     intSZ =s.length ();5     intprev[ -];6memset (prev,-1,sizeof(prev));7 8      for(inti =0; I < sz; i++) {9         if(prev[s[i]-'a'] >=Left ) {Tenleft = prev[s[i]-'a'] +1; One         } Aprev[s[i]-'a'] =i; -MaxLen = Max (MaxLen, I-left +1); -     } the     returnMaxLen; -}

Method Three:

Hashtable method: The method and method two is actually the same idea, but the method I do not use the array to save the character position, but through the Hashtable to save, and thus improve efficiency. The following code:

1 //method Three: Hash table2 intLengthOfLongestSubstring3 (strings) {3     if(S.length () <2)4         returns.length ();5     intmax_len=0;6map<Char,int> sub;//Hash Map7      for(intI=0, j=0; I<s.length (); + +i) {8         if(Sub.find (s[i])! =Sub.end ()) {9J=max (j,sub[s[i]]+1);Ten         } Onesub[s[i]]=i; AMax_len=max (max_len,i-j+1); -     } -     returnMax_len; the}

Leetcode:3_longest Substring without repeating characters | The length of the oldest string without repeating characters | Medium

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.