Use a sliding window with two pointers to maintain a window of unique characters, expanding the right pointer and shrinking the left when a duplicate is found. Track the maximum window length seen. This runs in O(n) time because each character is visited at most twice.
Pro tip: Clarify the problem statement first: if the string contains all unique characters, the answer is the entire string length. Also, mention that the sliding window approach is optimal and handles the 100k constraint efficiently.
Restate the problem to ensure understanding: find the longest substring without repeating characters. Confirm constraints: lowercase letters, length up to 100k, O(n) required.
Use a hash set or a fixed-size array (size 26) to track characters in the current window. A set is simpler; an array is faster and uses less memory.
Initialize left and right pointers at 0, and max_length = 0. Expand right, adding characters to the set. If a duplicate is found, shrink the window by moving left and removing characters until the duplicate is gone.
After each expansion, update max_length with the current window size. Continue until right reaches the end, then return max_length.
Explain that each character is processed at most twice (once by right, once by left), so time is O(n). Space is O(1) since the alphabet is fixed at 26.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.