3. Longest Substring Without Repeating Characters
Intuition
To find the longest substring without repeating characters, we can use the sliding window technique with two pointers (beginning and end) and a character frequency map. This allows us to efficiently track unique characters within our current window.
Approach
- Use a sliding window with two pointers:
beg
(start of window) andend
(end of window) - Maintain a frequency map (using an array of size 256 for ASCII characters) to track character occurrences
- For each character at the
end
pointer:- If the character hasn't been seen (frequency is 0), add it to our window and update max length
- If the character has been seen, shrink the window from the beginning until we remove the duplicate
- Continue this process until we reach the end of the string
Complexity
- Time complexity: O(n)
- Space complexity: O(1)
Keywords
- Sliding Window
- Two Pointers