forked from sashaaero/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leet_3.py
41 lines (37 loc) · 1.55 KB
/
leet_3.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
# https://leetcode.com/problems/longest-substring-without-repeating-characters
# Runtime: 88 ms, faster than 40.90% of Python3 online submissions for Longest Substring Without Repeating Characters.
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
max_possible_len = len(set(s))
if max_possible_len == len(s):
return max_possible_len
max_found_len = 0
for i, c in enumerate(s):
if max_found_len >= (len(s) - i):
return max_found_len
curr_sub = {c}
curr_max_len = 1
for j in range(i + 1, len(s)):
c2 = s[j]
if c2 not in curr_sub:
curr_sub.add(c2)
curr_max_len += 1
else:
if curr_max_len > max_found_len:
max_found_len = curr_max_len
if max_found_len == max_possible_len:
return max_found_len
break
else:
if curr_max_len > max_found_len:
max_found_len = curr_max_len
if max_found_len == max_possible_len:
return max_found_len
return max_found_len
if __name__ == '__main__':
s = Solution()
assert s.lengthOfLongestSubstring("abcabcbb") == 3
assert s.lengthOfLongestSubstring("bbbbbbbb") == 1
assert s.lengthOfLongestSubstring("pwwkew") == 3
assert s.lengthOfLongestSubstring(" ") == 1
assert s.lengthOfLongestSubstring("aab") == 2