leetcode

Given a string, find the length of the longest substring without repeating characters.python

Examples:app

Given "abcabcbb", the answer is "abc", which the length is 3.code

Given "bbbbb", the answer is "b", with the length of 1.string

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.it


   

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        l = list(s)
        if len(s)==0:
            return 0
        elif len(s)==1:
            return 1
        tmp = []
        result = []
        for i in l:
            if i not in tmp:
                tmp.append(i)
                result.append(len(tmp))
            else:
                result.append(len(tmp))
                index = tmp.index(i)
                while index>=0:
                    tmp.pop(index)
                    index = index -1
                tmp.append(i)
        if len(result)<=0:
            result.append(len(l))  

        return max(result)