Skip to content
DSA Patterns
Menu
Language
Sliding WindowLC #31 of 6Next →

Demo preview: These solutions pass the automated test suite but have not been human-reviewed. Treat trade-offs and prose as draft.

Longest Substring Without Repeating Characters

Problem (restated)

Given a string s, return the length of the longest substring that contains no repeating characters.

Intuition

The answer is a contiguous range → sliding window. Expand right; when a duplicate appears inside the window, advance left past the previous occurrence.

Approaches

Sliding window + last-seen index

Unverified
Time O(n)Space O(min(n, Σ))

Idea. Maintain window [left,right] with unique chars. Map char→last index. On duplicate in window, set left = max(left, lastIndex+1). Track max length.

Walkthrough. s=“abcabcbb”. Windows grow “a”,“ab”,“abc”, then second a forces left past first a → “bca”, … best=3.

Trade-offs. Linear time optimal. last-seen map is simpler than a frequency map for this problem.

Solution
export function lengthOfLongestSubstring(s: string): number {
  const last = new Map<string, number>();
  let left = 0;
  let best = 0;
  for (let right = 0; right < s.length; right++) {
    const ch = s[right]!;
    if (last.has(ch) && last.get(ch)! >= left) {
      left = last.get(ch)! + 1;
    }
    last.set(ch, right);
    best = Math.max(best, right - left + 1);
  }
  return best;
}
export function lengthOfLongestSubstring(s: string): number {
  const last = new Map<string, number>();
  let left = 0;
  let best = 0;
  for (let right = 0; right < s.length; right++) {
    const ch = s[right]!;
    if (last.has(ch) && last.get(ch)! >= left) {
      left = last.get(ch)! + 1;
    }
    last.set(ch, right);
    best = Math.max(best, right - left + 1);
  }
  return best;
}

Check all substrings

Unverified
Time O(n²)Space O(min(n, Σ))

Idea. For each start index, extend while chars are unique using a set.

Walkthrough. From each i expand until a repeat; track max length.

Trade-offs. Simple but quadratic. use only as a reference implementation.

Solution
export function lengthBrute(s: string): number {
  let best = 0;
  for (let i = 0; i < s.length; i++) {
    const seen = new Set<string>();
    for (let j = i; j < s.length; j++) {
      if (seen.has(s[j]!)) break;
      seen.add(s[j]!);
      best = Math.max(best, j - i + 1);
    }
  }
  return best;
}
export function lengthBrute(s: string): number {
  let best = 0;
  for (let i = 0; i < s.length; i++) {
    const seen = new Set<string>();
    for (let j = i; j < s.length; j++) {
      if (seen.has(s[j]!)) break;
      seen.add(s[j]!);
      best = Math.max(best, j - i + 1);
    }
  }
  return best;
}

Template connection

Max window of unique characters. last stores the previous index of each character. When that index is still inside the window, left jumps to the index after it. The answer is the largest right - left + 1.

Reflection

  • “No repeats, longest contiguous span” is the cue for a sliding window. Why is a last-seen index simpler than a frequency map?
  • If you walk left forward one step instead of jumping to lastIndex + 1, is the scan still O(n)?
  • All characters the same, and all distinct: the answers are 1 and n. Which end does an off-by-one hit?
Contributor review checklist

Interactive

Mental model

A worked animation for this problem. Scrub steps or press space to pause; re-tell the invariant out loud.

Step 1 of 9
1/9

Start with an empty window. Invariant: all characters unique.

a
0
b
1
c
2
a
3
b
4
b
5
StateL=0R=0best=0

best = 0

Key:CurrentActive rangeSettledEliminated