您的位置:首页 > 其它

Leetcode: Longest Substring Without Repeating Characters

2015-09-15 06:56 363 查看

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

[code]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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: