[Leetcode] [Python] Longest Substring without repeating characters

Source: Internet
Author: User

#-*-Coding:utf8-*-
‘‘‘
__author__ = ' [email protected] '
https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/

Given a string, find the length of the longest substring without repeating characters.
For example, the longest substring without repeating letters for "ABCABCBB" is "abc",
which the length is 3. For "bbbbb" the longest substring are "B", with the length of 1.

===comments by dabay===
Maintain a variable Max_so_far to record the maximum number of distinct string lengths so far, while maintaining a string that must contain the character being checked.
Because this string may continue to grow, update max_so_far when the length exceeds Max_so_far.
‘‘‘
Class Solution:
# @return An integer
def lengthoflongestsubstring (self, s):
If Len (s) <= 1:
Return Len (s)
Max_so_far = 0
Longest = ""
For Char in S:
If char in longest:
Longest = longest[(Longest.index (char)) + 1:] + Char
Else
Longest = longest + char
Max_so_far = Max (Max_so_far, Len (longest))
Return Max_so_far


def main ():
s = solution ()
string = "ABCABCBB"
Print s.lengthoflongestsubstring (String)


if __name__ = = "__main__":
Import time
Start = Time.clock ()
Main ()
Print "%s sec"% (Time.clock ()-start)

[Leetcode] [Python] 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.