-
Notifications
You must be signed in to change notification settings - Fork 0
/
q3_Longest_Substring.py
46 lines (42 loc) · 972 Bytes
/
q3_Longest_Substring.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution:
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
used = {}
l = i = 0
for j in range(len(s)):
if s[j] in used and i<used[s[j]]:
i = used[s[j]]
l = max(l,j-i+1)
used[s[j]] = j+1
return l
# t = Solution()
# s = "abcabcbb"
# print(t.lengthOfLongestSubstring(s))
# l = 0
# for i in range(len(s)):
# d = set()
# for j in range(i,len(s)):
# if s[j] in d:
# if j-i>l:
# l=j-i
# break
# elif j==len(s)-1:
# if j-i+1>l:
# l=j-i+1
# else:
# d.add(s[j])
# return l
# used = {}
# max_length = start = 0
# for i, c in enumerate(s):
# if c in used and start <= used[c]:
# start = used[c] + 1
# else:
# max_length = max(max_length, i - start + 1)
#
# used[c] = i
#
# return max_length