-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.longest-substring-without-repeating-characters.py
54 lines (44 loc) · 1.28 KB
/
3.longest-substring-without-repeating-characters.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
46
47
48
49
50
51
52
53
#
# @lc app=leetcode id=3 lang=python3
#
# [3] Longest Substring Without Repeating Characters
#
# @lc code=start
from tracemalloc import start
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
res = 0
length = len(s)
for i in range(len(s)):
ans = [s[i]]
j = 1
while i+j<length and s[i+j] not in ans:
ans.append(s[i+j])
j += 1
res = max(res, len(ans))
return res
def lengthOfLongestSubstring2(self, s: str) -> int:
start = maxLength = 0
usedChar = {}
for i in range(len(s)):
if s[i] in usedChar and start <= usedChar[s[i]]:
start = usedChar[s[i]] + 1
else:
maxLength = max(maxLength, i - start + 1)
usedChar[s[i]] = i
return maxLength
def lengthOfLongestSubstring3(self, s: str) -> int:
used = {}
res, start = 0, 0
for i in range(len(s)):
if s[i] in used:
start = max(start, used[s[i]] + 1)
res = max(res, i - start + 1)
used[s[i]] = i
return res
# @lc code=end
import time
time1 = time.time()
n = "tmmzuxt"
pro = Solution()
print(pro.lengthOfLongestSubstring3(n))