Skip to content

Commit 8bac4aa

Browse files
committed
[WEEK7]
Longest Substring Without Repeating Characters solution
1 parent 0d47f05 commit 8bac4aa

File tree

1 file changed

+22
-0
lines changed
  • longest-substring-without-repeating-characters

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
TC: O(n)
3+
SC: O(n)
4+
"""
5+
class Solution:
6+
def lengthOfLongestSubstring(self, s: str) -> int:
7+
str_list = []
8+
max_length = 0
9+
for i in range(len(s)):
10+
if s[i] not in str_list:
11+
str_list.append(s[i])
12+
else:
13+
if max_length < len(str_list):
14+
max_length = len(str_list)
15+
str_list = str_list[str_list.index(s[i])+1:]
16+
str_list.append(s[i])
17+
18+
if max_length < len(str_list):
19+
max_length = len(str_list)
20+
21+
return max_length
22+

0 commit comments

Comments
 (0)