Given the string "abcabcbb", find the length of the longest substring where no character repeats. The brute force is simple: start at each index, expand until you hit a duplicate, record the length, move to the next index. But something wasteful hides inside that loop. Drive it below and see if you can spot it.

The brute force approach starts at each index, expands until a duplicate appears, then moves to the next index. Each run builds a fresh set of unique characters from scratch. Think about what happens when consecutive runs overlap — when the second run starts at index 1, it checks characters that the first run already proved were unique.

Drive it yourself below — one tap is one character check, and every check lays a row.

Restart from every index.

One row per letter read. j is the reader; i is where this start began.

Run 0 begins at index 0 with an empty set. Tap to check the first character.
reads 0
Characters re-validated: 0

The loop you are driving:

1
for (let i = 0; i < s.length; i++) {
2
  const seen = new Set<string>();
3
  for (let j = i; j < s.length; j++) {
4
    if (seen.has(s[j])) break;
5
    seen.add(s[j]);
6
    best = Math.max(best, j - i + 1);
7
  }
8
}