Leetcode 3 python

Source: Internet
Author: User

Given a string, find the length of the oldest string that does not contain repeating characters.

Example:

Given "abcabcbb" that the oldest string without repeating characters is "abc" , then the length is 3.

Given "bbbbb" , the longest substring is, the "b" length is 1.

Given "pwwkew" that the eldest string is "wke" , the length is 3. Note that the answer must be a substring, "pwke" which is a subsequence and not a substring.

Answer:

First of all, the problem is to find the longest non-repeating substring, the non-repeating letters should be together with each other, the idea is to first initialize the definition of a longest non-empty string length of 1, followed by a substring, type string.

Iterates over the string given by the topic, encounters a non-repeating addition to the substring, if the letter exists in the substring, records the maximum length and starts with the next letter that repeats.

Attention:

Finally, don't forget to compare the substring to the maximum substring after the loop is finished.

Class solution (Object):    def lengthoflongestsubstring (self, s): "" "        : Type s:str        : Rtype:int        " ""        if not s:            return 0        substr= "        longeststr=1 for        S1 in S:            if S1 not in substr:                substr=substr +S1            Else:                If Len (substr) >longeststr:                    longeststr=len (substr)                substr=substr+s1                substr =substr[substr.index (S1) + 1:]        If Len (substr) >longeststr:            longeststr=len (substr)        return Longeststr                

  

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