Python solution Leetcode:3. Longest Substring without repeating characters

Source: Internet
Author: User

    • Title Description: The length of the longest continuous substring that asks for a string without repeating characters;

    • Ideas:
    1. Use a hash table to hold the position where the character appears;
    2. Use left and right to represent the leftmost and leftmost characters of the substring, respectively;
    3. Iterates through a string, if the current character is in a hash table and the current character is greater than left in the SID, indicating that there is the same character as the right index between left and right, at which point it is set to the value of the current value in the hash table;
    4. The current index value of +1, which is the number of characters, to find the current maximum length;
    5. Repeat 3-4 steps until the traversal is complete;

Feel this is a dynamic planning problem, temporarily useless dynamic planning analysis, and further.

classSolution (Object):defLengthoflongestsubstring ( Self, s):""": Type S:str: Rtype:int        """Hashes={} left, right, length= 0,0,Len(s) max_len= 0         whileRight<LengthifHashes.get (S[right]) andHashes[s[right]]>=Left:left=Hashes[s[right]] [Hashes[s[right]]=Right+ 1Max_len= Max(Max_len, right-Left+ 1) Right+= 1        returnMax_len

Python solution 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.