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