Skip to content

Commit abe1007

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

File tree

1 file changed

+16
-0
lines changed
  • python/leetcode3_longest_substring_without_repeating_characters

1 file changed

+16
-0
lines changed

python/leetcode3_longest_substring_without_repeating_characters/solution.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,19 @@ def lengthOfLongestSubstring(self, s:str)->int:
4040
right = right + 1
4141

4242
return res
43+
44+
# runtime 72ms beat 56%
45+
class Solution:
46+
def lengthOfLongestSubstring(self, s:str)->int:
47+
res, left, right = 0, 0, 0
48+
dic = {}
49+
50+
while right < len(s) and left + res < len(s):
51+
if s[right] in dic:
52+
left = max(left, dic[s[right]] + 1)
53+
54+
dic[s[right]] = right
55+
res = max(res, right - left + 1)
56+
right = right + 1
57+
58+
return res

0 commit comments

Comments
 (0)