Longest Substring Without Repeating Characters

 1 from collections import defaultdict
 2 class Solution(object):
 3     def lengthOfLongestSubstring(self, s):
 4         """
 5         :type s: str
 6         :rtype: int
 7         """
 8 
 9         dic=defaultdict(int)
10         j=0
11         res=0   
12 
13         for i in range(len(s)):
14             while(j<len(s) and dic[s[j]]==0):
15                 dic[s[j]]+=1
16                 j+=1
17             
18             # if(dic[s[i]>1]): # 当右边s[j]是重复字符时,最左端s[i]不一定恰巧是那个重复字符
19             #     dic[s[i]]-=1
20             dic[s[i]]-=1
21             
22             if(j-i>res):
23                 res=max(res,j-i)
24         
25         return res
原文地址:https://www.cnblogs.com/zijidan/p/12535244.html