SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Course Overview
Algorithm Patterns OverviewFrequency CountersTwo SumValid AnagramGroup AnagramsFrequency Counters and Hash-Map LookupTwo PointersValid PalindromeTwo Sum IIContainer With Most WaterTwo-Pointer ReasoningSliding WindowMaximum Average Subarray ILongest Substring Without Repeating CharactersMinimum Size Subarray SumSliding-Window Reasoning
Algorithm Patterns

Longest Substring Without Repeating Characters

Use a variable-size sliding window to find the longest unique substring.

Try It Yourself

Use the editor below to find the length of the longest substring with no duplicate characters.

Keep every character in the window unique

Expand from the right and shrink from the left only when the new character is already present. Handle empty strings, spaces, and repetitions after a gap.

Submit your implementation when it passes the examples.

Longest Substring Without Repeating Characters

mediumSliding Window · Strings · Hash Set30 minLC 3
Problem

Given a string s, return the length of its longest contiguous substring with no repeated characters.

The substring must use adjacent characters from the original string.

Example 1:

Input: s = "abcabcbb"
Output: 3
Explanation: abc has three distinct characters. Every run of four consecutive characters repeats a letter, so the maximum length is 3.

Example 2:

Input: s = "bbbbb"
Output: 1
Explanation: One b is a valid substring. Any longer substring contains another b, so the maximum length is 1.

Constraints

  • 0 ≤ s.length ≤ 50,000
  • s may contain ASCII letters, digits, spaces, and symbols.
0 attempts

Test results

Results appear here after a run.

Solution

In abcab, the valid window changes from abc to bca to cab as a repeated character arrives.

The left and right indexes mark the edges of the window, and a set tracks its characters. If s[right] is already in the set, remove s[left] and move left forward until the duplicate is gone.

Add s[right] and use right - left + 1 to update the longest valid length. For "abba", the second b requires removing both the old a and the old b before the new b can enter.

function lengthOfLongestSubstring(s) {
  const window = new Set();
  let left = 0;
  let longest = 0;

  for (let right = 0; right < s.length; right++) {
    while (window.has(s[right])) {
      window.delete(s[left]);
      left++;
    }

    window.add(s[right]);
    longest = Math.max(longest, right - left + 1);
  }

  return longest;
}

Big O notation

MeasureComplexityExplanation
TimeAverage O(n)Each character enters and leaves the window at most once.
Auxiliary spaceO(k)Here k is the maximum number of distinct characters in a valid window.

How is this practice?

Previous lessonMaximum Average Subarray INext lessonMinimum Size Subarray Sum
Edit on GitHubReport on GitHub
On this page
%

On this page

Try It YourselfSolutionBig O notation