Skip to content

Commit 2714bd7

Browse files
committed
longest substring without repeating characters solution
1 parent 72668e7 commit 2714bd7

File tree

1 file changed

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

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# space complexity O(n), time complexity O(1)
2+
class Solution:
3+
def lengthOfLongestSubstring(self, s: str) -> int:
4+
seen = {}
5+
left = 0
6+
max_length = 0
7+
8+
for right, char in enumerate(s):
9+
if char in seen and seen[char] >= left:
10+
left = seen[char] + 1
11+
seen[char] = right
12+
max_length = max(max_length, right - left + 1)
13+
14+
return max_length

0 commit comments

Comments
 (0)