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.
1 class Solution: 2 def lengthOfLongestSubstring(self, s): 3 """ 4 :type s: str 5 :rtype: int 6 """ 7 start = 0 8 maxlen = 0 9 used ={} 10 for i ,c in enumerate(s): 11 if c not in used or used[c] < start: 12 maxlen = max(maxlen,i-start + 1) 13 else: 14 start = used[c] + 1 15 used[c] = i 16 return maxlen