blob: 4390dd7d89d6ee9f07a72788e3aa9b0f97bfc447 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
def longestSubstring(s: str) -> int:
if s == "":
return 0
start = 0
end = 0
maxLength = 0
uniqueCharacter = set()
while end < len(s):
if s[end] not in uniqueCharacter:
uniqueCharacter.add(s[end])
end += 1
maxLength = max(maxLength, len(uniqueCharacter))
else:
uniqueCharacter.remove(s[start])
start += 1
return maxLength
#should output 3, as "abc" is the longest substring that occurs
print(longestSubstring("abcabcbb"))
|