AAlgoLoopSpaced repetition for LeetCode
MEDIUMSliding WindowLeetCode ↗

Longest Substring Without Repeating Characters

The key idea

Slide a window [left, right] over s. As right advances, if the new character is already inside the window, jump left just past its previous occurrence so the window never holds a duplicate. The best window length seen is the answer.

Problem

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

A substring is a contiguous run of characters within s. The substring you return must contain no character more than once. If s is empty, the answer is 0.

Constraints

Examples

Input: s = "abcabcbb" Output: 3
Input: s = "bbbbb" Output: 1
Input: s = "pwwkew" Output: 3

Complexity

Time: O(n) Space: O(min(n, m))

See the full solution

410310
Step-by-step visualization
Start free →

More Sliding Window problems