Longest Substring Without Repeating Characters

来源:互联网 发布:sql identity off 编辑:程序博客网 时间:2024/05/16 03:27

Given a string, find the length of the longest substring without repeating characters.
Examples:
Given “abcabcbb”, the answer is “abc”, which the length is 3.
Given “bbbbb”, the answer is “b”, with the length of 1.
Given “pwwkew”, the answer is “wke”, with the length of 3. Note that the answer must be a substring, “pwke” is a subsequence and not a substring.

class Solution:    def lengthOfLongestSubstring(self, s):        """        :type s: str        :rtype: int        """        maxlength = 0        length = 0        substring = []        for i in s:            try:                index = substring.index(i)                substring = substring[index+1:]                substring.append(i)                maxlength = max(maxlength, length)                length = length - index            except ValueError:                substring.append(i)                length += 1                maxlength = max(maxlength, length)        return maxlength
原创粉丝点击