Deviopath › Python

Longest Substring Without Repeats

Hard · Strings

A sliding window is a range [left, right] that moves over a string. You grow it on the right one character at a time, and shrink it from the left only when it breaks a rule. Each character enters and leaves the window at most once, so the whole scan is O(n).

Write longest_unique(s) that returns the length of the longest substring (a contiguous part) of s with no repeated characters.

Tip: remember the last index where you saw each character. When you see a repeat inside the window, move left just past the earlier copy.

Next: Top K Frequent Elements