[leetcode] Longest Substring Without Repeating Characters

来源:互联网 发布:淘宝贝幼儿园怎么样 编辑:程序博客网 时间:2024/06/10 08:36

Q: 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.

解题思路:动态规划

class Solution:    # @param {string} s    # @return {integer}     def lengthOfLongestSubstring(self, s):        if s is None or len(s)==0:            return 0        if len(s)==1:            return 1        dic = {}        maxcount,startindex = 0,-1        for index,i in enumerate(s):            if i in dic and startindex<dic[i]:                startindex = dic[i]            if index-startindex>maxcount:                maxcount = index-startindex            dic[i]=index        return maxcount
0 0