"""
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.
"""
class (object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
if s == '':
return 0
temp = [s[0]]
longest_num = 0
for i in s[1:]:
if i in temp:
if temp.__len__()>longest_num:
longest_num=temp.__len__()
if temp.index(i)==(temp.__len__()-1):
temp=[i]
else:
index = temp.index(i)
temp = temp[index+1:]
temp.append(i)
else:
temp.append(i)
if temp.__len__()>longest_num:
longest_num=temp.__len__()
return longest_num
if __name__ == '__main__':
sol = Solution()
num = sol.lengthOfLongestSubstring("anviaj")
print(num)
近期评论