Longest Substring Without Repeating Characters
#注意进出counter while的几个条件
import collections
class Solution:
"""
@param s: a string
@return: an integer
"""
def lengthOfLongestSubstring(self, s):
# write your code here
counter=start=end=d=0
map = collections.defaultdict(int)
n = len(s)
while end<n:
if map[s[end]] > 0:
counter+=1
map[s[end]]+=1
end+=1
while counter>0:
if map[s[start]]>1:
counter -=1
map[s[start]] -=1
start+=1
d=max(d,end-start)
return dLast updated