Return the length of the longest substring with all different characters.
Before you code
Decide what the left edge means: everything inside the window is duplicate-free. When the right end lands on a letter already inside the window, the left edge must jump past that earlier copy — and never slide backward.
Hints
- Keep a last-seen index for each character as the right end moves forward.
- When a repeat lands inside the window, move the left edge just past the earlier copy.
- Clamp the jump so an old sighting from before the window cannot drag the edge backward; only repeats at or past the edge matter.
Production & complexity notes
When solving Longest Substring Without Repeating Characters on LeetCode (medium), always state your loop or recursion invariant before writing code. Verify how your chosen data structure behaves on degenerate inputs (single-element inputs, all-duplicate keys, strictly monotonic sequences, or maximum constraint sizes up to $10^5$ elements). In production interviews, pair your implementation with an explicit time and auxiliary space complexity proof, explain why alternative brute-force approaches violate the latency budget, and trace how cache locality and memory allocation overhead affect wall-clock throughput on real hardware. Review the step-by-step memory trace in Sliding window to reinforce the underlying pattern.
What it practices
A window with a monotonic left edge, from Sliding window.