Medium · Sliding Window
Understand → Visualise → Practice.
Some windows grow and shrink depending on what they contain. Here the window must never hold a repeated character.
Grow the window one character at a time. When a character repeats, jump the start just past its previous position.
Map each character to its latest index.
Only repeats inside the current window matter.
start = last[c] + 1, then record the window length.
O(n) time. Space is bounded by the number of distinct characters.
A character seen before the window started is not a repeat; check last[c] ≥ start.
Read the full guides first: Big-O and Complexity, Arrays and Dynamic Arrays, Prefix Sums and Difference Arrays.
Guest mode lets you understand, visualise and run the sample tests. Create a free account to submit and save your solutions.