Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def lengthOfLongestSubstring(self, s: str) -> int:
- """ Thought: We can have two pointers, with a set listing
- all the characters contained in the span between them. We
- advance the right pointer until we hit a duplicate, at
- which point we advance the left pointer. We use a separate
- variable to keep track of the largest substring we've
- encountered.
- """
- l = 0
- r = 0
- if len(s) == 0:
- return 0
- span_set = set([s[0]])
- largest_substring = 1
- while r < len(s) - 1:
- r += 1
- while s[r] in span_set:
- span_set.remove(s[l])
- l += 1
- span_set.add(s[r])
- largest_substring = max(largest_substring, r - l + 1)
- return largest_substring
Advertisement
Add Comment
Please, Sign In to add comment