Practice

Longest Substring Without Repeating Characters

Module 11 · Sliding Window

Problem

Given a string s, return the length of the longest substring without repeating characters.

Examples

Example 1

Input"abcabcbb"Output3

Explanation. abc

Example 2

Input"bbbbb"Output1

Explanation. b

Example 3

Input"pwwkew"Output3

Explanation. wke

Constraints

0 ≤ n ≤ 5·10⁴ · letters, digits, symbols, spaces.

Attempt it first

The dynamic-window lesson's "longest, upper-bound" template, mirrored against Minimum Size Subarray Sum's "shortest, lower-bound" shape. Validity here is "no duplicate in the window" — figure out what state answers "is the incoming character already in the window?" in O(1), and what invalidates the window when it arrives.