mediumSliding WindowPattern: Sliding Window / Hash Set
Longest Unique Segment Solution
Problem Statement
Given an array of integers `packetTypes`, find the length of the longest continuous segment with no repeating elements.
Examples
Example 1:
Input:abcabcbb
Output:3
Explanation: The longest continuous segment of packet types with no repetition is 'abc' with a length of 3
Example 2:
Input:bbbbbb
Output:1
Explanation: The longest continuous segment of packet types with no repetition is 'b' with a length of 1
Constraints
- 0 <= s.length <= 5 * 10^4
- s consists of English letters, digits, symbols and spaces.
Time: O(N) Space: O(min(N, M))
Sliding window with left and right pointers. Add s[right] to set. If it exists in set, remove s[left] from set and increment left until the duplicate is gone. Update maxLength. Time O(N), Space O(min(N, charset)).
Run, Test & Submit Code
Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.
Solve on Interactive WorkspaceTested Solutions
No solution code is currently loaded.
Complete this code in the workspace editor.
