Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def lengthOfLongestSubstring(self, s: str) -> int:
- """
- At each step, right pointer points to a locations where we
- are trying to calculate length of longest string ending there.
- When we are iterating over the character corresponding to right pointer,
- we might have added one of the duplicate characters already in the hashset
- so we have to remove characters from left untill duplicate of s[right] is removed
- Think of it this way:
- how will you calcuate the longest substring ending at aright pointer
- """
- left = 0
- hashset = set()
- ans = 0
- for right in range(len(s)):
- while(s[right] in hashset):
- hashset.remove(s[left])
- left=left+1
- hashset.add(s[right])
- ans = max(ans, right - left + 1)
- return ans
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement