Given a string, find the length of the longest substring without repeating characters.
Examples:
Given “abcabcbb”, the answer is “abc”, which the length is 3.
Given “bbbbb”, the answer is “b”, with the length of 1.
Given “pwwkew”, the answer is “wke”, with the length of 3. Note that the answer must be a substring, “pwke” is a subsequence and not a substring.
Code
classSolution(object):deflengthOfLongestSubstring(self, s):"""
:type s: str
:rtype: int
"""
max=0
curmax=0
substr=""#当前的子字符串
i=0
length = len(s)
while i<length:
ind = substr.find(s[i])
if ind==-1:#no containing item
substr+=s[i]
curmax+=1else:
if max < curmax:
max = curmax
i = i - len(substr) + ind #回溯
substr=""
curmax = 0
i+=1if max < curmax:
max = curmax
return max
3.Longest Substring Without Repeating Characters-python
Given a string, find the length of the longest substring without repeating characters.Examples:Given “abcabcbb”, the answer is “abc”, which the length is 3.Given “bbbbb”, the answer is “b”, with the le