Editor

Longest Substring Without Repeats

#100 · Sliding window · Intermediate · 80 XP · about 15 min

Write longest_unique(s) that returns the length of the longest substring of s in which no character appears twice. A substring is a run of characters next to each other. Upper and lower case letters are different characters, and spaces, digits and punctuation count as characters too.

Examples

longest_unique('python3pp')   # returns 7
longest_unique('aaaa')        # returns 1
longest_unique('swiss')       # returns 3
longest_unique('')            # returns 0

In 'python3pp', the run 'python3' uses seven different characters, and adding the next p would repeat one.

Constraints

  • 0 <= len(s) <= 50,000

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.