Skip to content

48. Longest Substring Without Repeats

Difficulty: Medium · Topic: DSA for data engineers

Write longest_unique(s): the length of the longest substring of s (a run of consecutive characters) in which no character repeats. It must be O(n): strings can be 200,000 characters long.

Your code runs in CPython on our servers against 7 tests, including hidden ones.

Sample test

longest_unique("abcabcbb")

Expected: 3

Hints

Hint 1Keep a window [start, i] with no repeats and grow it one character at a time.
Hint 2Remember each character's last index. When the new character was last seen inside the window, move start just past that position.
Hint 3Never move start backwards: a repeat that is already outside the window does not matter.

Learn the concepts

More Python problems

All Python problems