← All Exercises

Longest Substring Without Repeating Characters

intermediatejavascript~25min

Background

Sliding windows are the standard technique for longest-range problems with a validity constraint and appear in rate limiting and stream processing.

Problem

Given a string s, return the length of the longest contiguous substring that does not contain a repeating character.

Input

A string s of length 0 to 100000 made of printable ASCII characters.

Output

An integer length; return 0 for the empty string.

Sample tests

Sample 1

Input: s = "abcabcbb"
Expected: 3

Sample 2

Input: s = "bbbbb"
Expected: 1

Sample 3

Input: s = "pwwkew"
Expected: 3

Your solution

Learning objective

Maintain a window with a last-seen map and reason about why each index enters and leaves the window once.