题目:
请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度。假设字符串中只包含从’a’到’z’的字符。
样例
输入:“abcabc”
输出:3
解答:
class Solution:
def longestSubstringWithoutDuplication(self, s):
"""
:type s: str
:rtype: int
"""
d = {}
maxLength, start, curLength = 0, 0, 0
for i in range(len(s)):
if s[i] not in d:
d[s[i]] = i
curLength += 1
else:
for j in range(start, d[s[i]]):
del d[s[j]]
start = d[s[i]] + 1
d[s[i]] = i
maxLength = max(maxLength, curLength)
curLength = i - start + 1
maxLength = max(maxLength, curLength)
return maxLength