Given a string s, find the length of the longest substring without duplicate characters.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "zxyzxyz"
Output: 3 Explanation: The string "xyz" is the longest without duplicate characters.
Example 2:
Input: s = "xxxx"
Output: 1
In [6]:
def lengthOfLongestSubstring(s):
l=0
res=0
subs=set()
for r in range(len(s)):
while s[r] in subs:
subs.remove(s[l])
l+=1
subs.add(s[r])
res= max(res, len(subs))
return len(subs)
In [7]:
lengthOfLongestSubstring('zxyzxyz')
Out[7]:
3