Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Given a string s, find the length of the longest substring without repeating characters.
- Input: s = "abcabcbb"
- Output: 3
- left pointer 0
- right pointer 0
- expand right pointer till we can make substring with non repeating
- abcabcbb
- left = 0
- right = 0
- hashmap -> (a 1,b 1,c 1)
- abcabcbb
- 3
- a | bc | a
- bacabc
- left = 2
- right = 4
- left
- right
- I try to maximize window by exapnding right pointers toward right
- shrink by increamsing left
- '''
- from collections import defaultdict
- def longestSubstring(s) -> int:
- freqCount = defaultdict(int)
- left = 0
- maxSubstring = 0
- for right in range(len(s)):
- c = s[right]
- # shrink
- while (freqCount[c] > 0):
- freqCount[s[left]] -=1
- left += 1
- freqCount[c] += 1
- maxSubstring = max(maxSubstring, right - left + 1)
- return maxSubstring
- s = "aaaaa"
- print(longestSubstring(s))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement