Skip to content
Preptima
mediumCodingEntryMidSenior

Find the length of the longest substring without repeating characters.

2 min readUpdated 2026-08-15

Answer

Short answer

Maintain a sliding window with a map from character to its last seen index; when a duplicate appears inside the current window, jump the left boundary past that previous occurrence rather than shrinking one step at a time, giving a single-pass O(n) solution.

Was this answer helpful?

Likely follow-ups

  • How would you return the substring itself rather than its length?
  • What changes if the input is a Unicode string with surrogate pairs?
  • How would you generalise this to at most k distinct characters?
  • Can you achieve O(1) extra space, and under what assumption?

Related questions

Related Topics