Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Find the longest substring without repeating characters

Find the length of the longest substring in a given string that contains no repeating characters using the sliding window technique.

Python practice15 minTwo Pointers & Sliding WindowIntermediateLast updated March 26, 2026

Problem statement

Given a string s, return the length of the longest substring that contains no repeating characters. Use an efficient sliding-window approach that expands and shrinks the window while tracking seen characters to maintain uniqueness within the current window.

Task

Apply sliding window with character-index tracking to obtain an O(n) solution that finds the maximum-length substring without repeating characters.

Examples

Example 1

Input

s = 'abcabcbb'

Output

3

The longest substrings without repeating characters are 'abc' with length 3.

Input format

Single argument: a string s.

Output format

Return an integer representing the length of the longest substring without repeating characters.

Constraints

0 <= len(s) <= 10^5. Characters can be any valid Python characters. Time target: O(n), extra space: O(min(n, alphabet)).

Samples

Sample 1

Input

s = 'pwwkew'

Output

3

The longest substring without repeating characters is 'wke' (or 'kew'), length 3.