Leetcode 3.Longest Substring without repeating characters

Source: Internet
Author: User

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.

Using dynamic solver: For a known string s[1, ..., K], use a hash table table[256] record s[i] where it appears, starting with-1.

Assuming that the longest non-repeating length of a substring with the suffix s[k] is known to be suffix_max[k], then for S[1, ..., k+1],

A) if table[s[k+1]] is-1, indicating s[k+1] first appearance, suffix_max[k+1] = suffix_max[k] + 1;

b) Otherwise, use Last_visit record s[k+1] Last occurrence position, if Last_visit <= Table[s[i]],

1 intLengthoflongestsubstring (strings)2     {3         inttable[ the];4         intSuffix_max =0;//Suffix_max contains the maximum length of the suffix5         intGlobal_max =0;6         intLast_visit =0;//last time7         8memset (table,-1, the*sizeof(int));9          for(inti =0; I < s.size (); i++)Ten         { One             if(Table[s[i]] = =-1)//Not appear before A             { -suffix_max++; -             } the             Else -             { -                 if(Last_visit <=Table[s[i]]) -                 { +Suffix_max = i-Table[s[i]]; -Last_visit = Table[s[i]] +1; +                 } A                 Else at                 { -suffix_max++; -                 } -             } -Hash[s[i]] =i; -             if(Global_max <Suffix_max) inGlobal_max =Suffix_max; -         } to          +         returnGlobal_max; -}

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.