#3

Longest Substring Without Repeating Characters

Medium Variable window

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Return the length of the longest substring with no repeated characters.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: the longest stretch with no repeated letter.

Why this works: grow the window to the right; when a letter repeats, jump the left edge past its previous position.

Example: "abcabcbb"

"abc" length 3next 'a' repeats → left moves past first 'a' → "bca"… best stays 3

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Moving left backwards when the previous occurrence is before the window. Use max(left, last + 1).

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(charset) — 128 for ASCII.
Topic: Sliding Window
🎬 Episode 10: video coming soon