TRT
Sign in
mediumAdvancedLeetCode#sliding-window#last-seen-index

Longest Substring Without Repeating Characters

Find the maximum length of a contiguous substring containing no repeated characters.

The Runtime Theory Team25 min read
Taught inSliding windowPro — members onlyA window over a contiguous range grows on the right and shrinks on the left, with both ends only ever moving forward. Keeping a summary of the window up to date turns many O(n²) substring and subarray searches into O(n).
Solve it

Solving happens on the judge — come back and mark it done

Sample cases

ins="abcabcbb"

out3

ins="bbbbb"

out1

ins="pwwkew"

out3

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

  1. Keep a last-seen index for each character as the right end moves forward.
  2. When a repeat lands inside the window, move the left edge just past the earlier copy.
  3. 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.

Not started

Sign in to save your learning progress.

Sign in to save