Skip to content

Commit 6332e1e

Browse files
committed
longest substring without repeating characters python solution
1 parent 054c677 commit 6332e1e

File tree

1 file changed

+24
-0
lines changed
  • python/leetcode3_longest_substring_without_repeating_characters

1 file changed

+24
-0
lines changed

python/leetcode3_longest_substring_without_repeating_characters/solution.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# runtime 1184ms beat 5%
12
class Solution:
23
def lengthOfLongestSubstring(self, s:str)->int:
34
res, left, right = 0, 0, 0
@@ -16,3 +17,26 @@ def lengthOfLongestSubstring(self, s:str)->int:
1617
setttings.clear()
1718

1819
return res
20+
21+
# runtime 68ms beat 64%
22+
class Solution:
23+
def lengthOfLongestSubstring(self, s:str)->int:
24+
res, left, right = 0, 0, 0
25+
settings = set()
26+
27+
while right < len(s):
28+
if s[right] in settings:
29+
while left < right:
30+
if s[left] == s[right]:
31+
settings.discard(s[left])
32+
left = left + 1
33+
break
34+
else:
35+
settings.discard(s[left])
36+
left = left + 1
37+
38+
settings.add(s[right])
39+
res = max(res, right - left + 1)
40+
right = right + 1
41+
42+
return res

0 commit comments

Comments
 (0)