Coding · must-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix

Longest Substring Without Repeating Characters

mediumsliding-windowhash-map

Problem

Given a string s, return the length of the longest contiguous substring in which no character appears more than once.

  • s has at most 60,000 characters and may be empty.
  • Any printable ASCII character can appear, including spaces and punctuation.

Examples

  • s = "xyzxyzyy" → 3. "xyz" (and later "zxy") is as long as a distinct run gets; the fourth character repeats the first.
  • s = "ggggg" → 1. Any two adjacent characters are equal.
  • s = "kvvzpakl" → 6. "vzpakl" has six distinct characters; extending it left would add a second v.

Hints

  1. If a substring is valid, every substring inside it is valid too. What does that suggest about how a window should grow and shrink?
  2. When the character at the right end repeats one already in the window, where must the left end move to?
  3. Sliding window with a map from character to its most recent index; jump the left boundary straight past the previous occurrence.

Last reviewed 8 September 2026