-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.longest-substring-without-repeating-characters.py
55 lines (48 loc) · 1.39 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
54
55
# 230327
def lengthOfLongestSubstring(s: str) -> int:
unique_chars = set()
longest = 0
start, end = 0, 0
while end < len(s):
old_size = len(unique_chars)
unique_chars.add(s[end])
new_size = len(unique_chars)
longest = max(longest, new_size)
if new_size == old_size:
start += 1
end = start
unique_chars = set()
continue
end += 1
return longest
input = "pwwkew"
print(lengthOfLongestSubstring(input))
input = "abcabcbb"
print(lengthOfLongestSubstring(input))
input = "dvdf"
print(lengthOfLongestSubstring(input))
def ba_lengthOfLongestSubstring(self, s: str) -> int:
seen = {}
l = 0
output = 0
for r in range(len(s)):
"""
If s[r] not in seen, we can keep increasing the window size by moving
right pointer
"""
if s[r] not in seen:
output = max(output,r-l+1)
"""
There are two cases if s[r] in seen:
case1: s[r] is inside the current window, we need to change the window
by moving left pointer to seen[s[r]] + 1.
case2: s[r] is not inside the current window, we can keep increase the
window
"""
else:
if seen[s[r]] < l:
output = max(output,r-l+1)
else:
l = seen[s[r]] + 1
seen[s[r]] = r
return output