-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.py
More file actions
44 lines (28 loc) · 928 Bytes
/
3.py
File metadata and controls
44 lines (28 loc) · 928 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import unittest
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
n = len(s)
left = 0
max_length = 0
hashset = set()
for right in range(n):
char = s[right]
while char in hashset:
leftmost_char = s[left]
hashset.remove(leftmost_char)
left += 1
hashset.add(char)
max_length = max(max_length, right - left + 1)
return max_length
class Tests(unittest.TestCase):
def test_one(self):
s = "abcabcbb"
self.assertEqual(Solution().lengthOfLongestSubstring(s), 3)
def test_two(self):
s = "bbbbb"
self.assertEqual(Solution().lengthOfLongestSubstring(s), 1)
def test_three(self):
s = "pwwkew"
self.assertEqual(Solution().lengthOfLongestSubstring(s), 3)
if __name__ == "__main__":
unittest.main()