#-*-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