Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def lengthOfLongestSubstring(self, s: str) -> int:
- """ How to solve it: Sliding window: Maintain a set of characters in the current window. Advance the
- right pointer as long as you don't have any duplicates. As soon as you have a duplicate, advance the left
- pointer until the duplicate is removed.
- """
- current_window_chars = set()
- res = 0
- l = 0
- for r in range(len(s)):
- while s[r] in current_window_chars:
- current_window_chars.remove(s[l])
- l += 1
- current_window_chars.add(s[r])
- res = max(res, r - l + 1)
- return res
Advertisement
Add Comment
Please, Sign In to add comment