例
Input:
s = "pwwkew"
Output:
3
Explanation:
The answer is "wke", with the length of 3.
CODE
def lengthOfLongestSubstring(s):
left, max_length = 0, 0
substring = set()
for right in range(len(s)):
if s[right] not in substring:
substring.add(s[right])
max_length = max(max_length, right - left + 1)
else:
# ifではなくwhile
while s[right] in substring:
substring.remove(s[left])
left += 1
# 結局、右要素をaddする
substring.add(s[right])
return max_length
set()
で初期化ができて、add
・remove
で処理する。