-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.py
29 lines (28 loc) · 779 Bytes
/
3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution:
# @param {string} s
# @return {integer}
def lengthOfLongestSubstring(self, s):
stack = []
max_sublen = 0
for c in s:
if c not in stack:
stack.append(c)
max_sublen = max(max_sublen, len(stack))
else:
idx = stack.index(c)
stack = stack[idx+1:]
stack.append(c)
return max_sublen
# new solution
def lengthOfLongestSubstring(self, s):
i = 0
smap = {}
max_len = 0
n = len(s)
for k in range(n):
c = s[k]
if c in smap:
i = max(smap[c]+1, i)
smap[c] = k
max_len = max(max_len, k-i+1)
return max_len