Leetcode longest Substring without repeating characters python

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.

Problem-Solving ideas: Use the Find function to find out if there is already a letter in the string to find, if so, then choose to count the new string from the next letter in the string to find the letter, of course, at this time to determine whether the string you have looked for is longer than the existing string. Also take care to handle the last found string.

Eg:s= "Echocomeon" to find the string tmp= "Echoc", when looking for s[5]= ' o ', there is already ' o ' in the string tmp, check len (TMP) and the length of the existing string, and update the new string tmp= "co".

1 classSolution:2     #@return An integer3     deflengthoflongestsubstring (self, s):4Length=Len (s)5         if(length==0):6             return07         elif(length==1):8             return19         Else:Ten             #Work with the data one by one Onetmp="" Amax_len=0 -str_len="" -              forIinchRange (length): theindex=Tmp.find (s[i]) -                 if(index>-1): -                     if(LEN (TMP) <Max_len): -tmp=tmp[(index+1):]+S[i] +                     Else: -max_len=Len (TMP) +tmp=tmp[(index+1):]+S[i] A                 Else: attmp=tmp+S[i] -             if(LEN (TMP) >Max_len): -max_len=Len (TMP) -             Else: -                 Pass -             returnMax_len

Leetcode longest Substring without repeating characters python

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.