Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def lengthOfLongestSubstring(self, s: str) -> int:
- # base case
- if not len(s):
- return 0
- hashset = set()
- left = 0; right = 0
- max_length = 0
- while(right < len(s)):
- if s[right] not in hashset:
- # keep adding the character untill we find duplicate
- hashset.add(s[right])
- max_length = max(max_length, right - left + 1)
- right += 1
- else:
- # once duplicate is found, keep removing elements from the left and update
- # the left pointer untill the repeating character is removed and we have
- # moved the left pointer to the next element of the duplicate character
- while(s[right] in hashset):
- hashset.remove(s[left])
- left += 1
- max_length = max(max_length, right - left + 1)
- return max_length
Advertisement
Add Comment
Please, Sign In to add comment