All problems

Longest Substring Without Repeating Characters

Medium · sliding window

Given a string s, find the longest substring in which no character appears twice, and return its length. A substring is a run of neighbouring characters, so "bcd" is a substring of "abcde" but "ace" is not.

The string can be empty, in which case the answer is 0.

Examples

Example 1

Input: s = "abcabcbb"

Output: 3

The longest repeat-free run is "abc" (length 3). Any longer run contains a repeated letter.

Example 2

Input: s = "bbbbb"

Output: 1

Every run longer than one character repeats the b.

Example 3

Input: s = "pwwkew"

Output: 3

"wke" works. "pwke" does not count, because it skips over a character and is not a substring.

Constraints