Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Find the length of the longest substring without repeating characters

Compute the length of the longest substring with all unique characters using a sliding window and index map.

Python practice14 minStrings – Sliding Window & FrequencyIntermediateLast updated March 25, 2026

Problem statement

Given a string s, return the length of the longest substring that contains no repeating characters. Use a sliding window and track characters' last seen positions to adjust the left boundary efficiently.

Task

Use a dynamic sliding window and a map of last-seen indices to maintain a window of unique characters and compute the maximum length.

Examples

Basic example

Input

s = "abcabcbb"

Output

3

The longest substring without repeating characters is "abc", with length 3.

Input format

One string argument: s. Example: length_of_longest_substring("pwwkew")

Output format

An integer representing the length of the longest substring without repeating characters.

Constraints

0 <= len(s) <= 10^5. Characters can be any valid string characters. Aim for O(n) time and O(min(n, alphabet)) space.

Samples

Sample 1

Input

s = "bbbbb"

Output

1

The longest substring without repeating characters is "b".