Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c46c1bb

Browse files
committedNov 29, 2019
longest substring without repeating characters python solution
1 parent abe1007 commit c46c1bb

File tree

1 file changed

+20
-0
lines changed
  • python/leetcode3_longest_substring_without_repeating_characters

1 file changed

+20
-0
lines changed
 

‎python/leetcode3_longest_substring_without_repeating_characters/solution.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,23 @@ def lengthOfLongestSubstring(self, s:str)->int:
5656
right = right + 1
5757

5858
return res
59+
60+
# runtime 44ms beat 99.32%
61+
class Solution:
62+
def lengthOfLongestSubstring(self, s:str)->int:
63+
res, start = 0, 0
64+
dic = {}
65+
66+
for i, value in enumerate(s):
67+
if value in dic:
68+
sums = dic[value] + 1
69+
if sums > start:
70+
start = sums
71+
72+
num = i - start + 1
73+
if num > res:
74+
res = num
75+
76+
dic[value] = i
77+
78+
return res

0 commit comments

Comments
 (0)
Please sign in to comment.