AlgoViz
← All problems

Longest Substring Without Repeating Characters

Medium+110 XPteaches: Sliding Window

Given a string `s`, return the LENGTH of the longest substring that contains no repeating characters. A substring is a contiguous run of characters (no gaps).

Example: s = "abcabcbb" → 3 ("abc")

Brute force: For every start index, extend right until a character repeats. (time O(n²), space O(n))

No visualization loaded.

Watch

i

Press Run to begin.

Why the best approach wins

Brute force re-scans every possible start from scratch — O(n²). The sliding window never looks back: L and R each only move right, and remembering the last index of each character lets L leap straight past a repeat instead of inching forward. That turns the whole thing into one O(n) pass.

Brute force: O(n²) time / O(n) spaceSliding window: O(n) time / O(k) space

Your turn — implement lengthOfLongestSubstring

Loading editor…