Leetcode刷题记录——面试题48. 最长不含重复字符的子字符串

Leetcode刷题记录——面试题48. 最长不含重复字符的子字符串

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        length = len(s)
        if length <= 1:
            return length
        newlist = [1]
        maxa = 1
        hashdict = {s[0]:0}
        for i in range(1,length):
            this = s[i]
            if this in hashdict and hashdict[this] != length:
                old = hashdict[this]
                newlist.append(i - old)
                for key in hashdict:
                    if hashdict[key] < old:
                        hashdict[key] = length
                hashdict[this] = i
            else:
                hashdict[this] = i
                newlist.append(newlist[-1] + 1)
                maxa = max(newlist[-1],maxa)
        return maxa

猜你喜欢