003. longest substring without repeating characters

Py3 - Solution

1
2
3
4
5
6
7
8
9
10
11
12
class :
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
list_s = list(s)
for i in range(len(set(list_s)),-1,-1):
for j in range(0,len(s)-i+1):
if len(set(list(s[j:j+i]))) == i:
return i
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class :
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
finded = {}
start = 0
max_length = 0
for i in range(len(s)):
if s[i] in finded and finded[s[i]] >= start:
start = finded[s[i]]+1
else:
if i - start + 1 > max_length:
max_length = i - start + 1
finded[s[i]] = i
return max_length

解决方案