-
Notifications
You must be signed in to change notification settings - Fork 0
/
LongestSubstring.py
29 lines (26 loc) · 982 Bytes
/
LongestSubstring.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Difficulty: Medium
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
"""
Takes s as input and returns the longest substring without repeated characters
Approach:
Maintain a dictionary of used charcters. Read characters in string s.
Iterate through s. Maintain last position you saw the current character and return
the max of previous max length and current non-repeated substring
args:
s: string
return:
maxLength: int
"""
start = maxLength = 0
usedChar = {}
for i in range(len(s)):
# If s[i] is already in current substring
if s[i] in usedChar and start <= usedChar[s[i]]:
# Update start
start = usedChar[s[i]] + 1
else:
# Update maxLength
maxLength = max(maxLength, i - start + 1)
usedChar[s[i]] = i
return maxLength