Skip to content

Commit 5667bbf

Browse files
committed
Solve problem
1 parent 8d6858c commit 5667bbf

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed
Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
1+
import collections
2+
3+
14
class Solution:
25
def lengthOfLongestSubstring(self, s):
36
"""
47
:type s: str
58
:rtype: int
69
"""
710
ans = 0
11+
812
if not s:
913
return ans
1014

11-
n = len(s)
12-
F = {} # frequency for every char between `left`, `right`
13-
rep = 0 # contained repeated char between `left`, `right`
15+
freqs = collections.defaultdict(int)
16+
i = rep = 0
1417

15-
left = right = 0
16-
while right < n:
17-
F[s[right]] = F.get(s[right], 0) + 1
18-
if F[s[right]] == 2:
18+
for j in range(len(s)):
19+
if freqs[s[j]] == 1:
1920
rep += 1
20-
right += 1
21+
freqs[s[j]] += 1
2122

2223
while rep > 0:
23-
if F[s[left]] == 2:
24+
freqs[s[i]] -= 1
25+
if freqs[s[i]] == 1:
2426
rep -= 1
25-
F[s[left]] -= 1
26-
left += 1
2727

28-
if right - left > ans:
29-
ans = right - left
28+
i += 1
29+
30+
ans = max(ans, j - i + 1)
3031

3132
return ans

0 commit comments

Comments
 (0)