-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0003.py
More file actions
29 lines (22 loc) · 707 Bytes
/
0003.py
File metadata and controls
29 lines (22 loc) · 707 Bytes
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
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
if len(set(s)) == len(s):
return len(s)
start, end = 0, 0
unique = set()
max_length = 0
while end < len(s):
if s[end] not in unique:
unique.add(s[end])
end += 1
max_length = max(max_length, len(unique))
else:
unique.remove(s[start])
start += 1
return max_length
if __name__ == "__main__":
s = Solution()
# r = s.lengthOfLongestSubstring("aab")
# r = s.lengthOfLongestSubstring("abcabcbb")
r = s.lengthOfLongestSubstring("pwwkew")
print(r)