Leetcode: Longest Substring Without Repeating Characters

来源:互联网 发布:java电信资费系统 编辑:程序博客网 时间:2024/06/07 07:22

Question

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 is “b”, with the length of 1.

Hide Tags Hash Table Two Pointers String
Hide Similar Problems (H) Longest Substring with At Most Two Distinct Characters


My Solution

class Solution(object):    def lengthOfLongestSubstring(self, s):        """        :type s: str        :rtype: int        """        if s=='':            return 0         res, start = 0, 0        dictn = {}        for ind in range(len(s)):            if s[ind] not in dictn:                dictn[s[ind]] = 1                res = max(res, len(dictn))            else:                while s[start]!=s[ind]:                    del dictn[s[start]]                    start += 1                start += 1        return res
0 0
原创粉丝点击