-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0003_longest_substring_without_repeating.py
More file actions
51 lines (45 loc) · 1.58 KB
/
0003_longest_substring_without_repeating.py
File metadata and controls
51 lines (45 loc) · 1.58 KB
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
46
47
48
49
50
51
import time
start_time = time.time()
# s = "pwwkew"
# s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
# s = "oelvjrrrazkwqgotdrwyevqyveanupskteiexzvxyfvqmmkrfipkdfkammpieisxmaclczlcfgvtsfkypcksjwsxlifpqaoe"
# s = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbfdfdfdfdfoe"
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
e = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ "
if len(e)>len(s):
_len = len(s)
else:
_len = len(e)
_lens = len(s)
# print(len(n))
# print(_len)
repeatedstr = ""
while _len > 0:
# print('--')
# print(_len)
for i in range(_lens-_len+1):
ss = s[i:_len+i]
# print(ss)
aa = []
repeated=False
for a in ss:
# print(a,ss,sep=" - ")
if a in aa:
repeated = True
break
else:
aa.append(a)
if not repeated:
repeatedstr = ss
break
if repeatedstr != "":
break
_len -= 1
return len(repeatedstr)
q = Solution()
r = q.lengthOfLongestSubstring(s)
print(r)
print("")
print("---")
print(time.time() - start_time, "seconds")