-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path003_substrXrepeat.py
More file actions
33 lines (21 loc) · 941 Bytes
/
Copy path003_substrXrepeat.py
File metadata and controls
33 lines (21 loc) · 941 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
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
dict_sub = {}
n = len(s)
if n == 1:
return 1
left = 0
len_sub = 0
for right in range(0, n):
if s[right] in dict_sub:
left = max(left, dict_sub[s[right]] + 1)
len_sub = max(len_sub, right - left + 1)
dict_sub[s[right]] = right
return len_sub
print(Solution().lengthOfLongestSubstring(s = "abcabcbb")) #3
print(Solution().lengthOfLongestSubstring(s = "bbbbb")) #1
print(Solution().lengthOfLongestSubstring(s = "pwwkew")) #3
print(Solution().lengthOfLongestSubstring(s = "dvdf")) #3
print(Solution().lengthOfLongestSubstring(s = "")) #0
print(Solution().lengthOfLongestSubstring(s = " ")) #1
print(Solution().lengthOfLongestSubstring(s ="tmmzuxt")) #5