Longest substring without repeating characters
Medium25 minFree, no account
The canonical sliding window, and the one-line optimisation that makes it genuinely linear.
The question
Given a string, return the length of the longest substring with no repeated characters.
Then return the substring itself.
longest("abcabcbb") // 3 "abc"
longest("bbbbb") // 1 "b"
longest("pwwkew") // 3 "wke", substring, not subsequence
longest("") // 025:00Commit to an answer before you open the solution. Reading it first teaches you to recognise good answers, which is not the skill being tested.
Stuck?
0 of 3 hints takenThe worked solution
written by a person · not a gradeScore yourself
0 of 5 marked- A correct sliding window in one pass30
- Handled the previous-occurrence-outside-window case30
- Returned the substring as well as the length15
- Justified O(n) rather than asserting it15
- Handled empty and single-character input10
We run no AI here and nothing on this page grades you. The score is yours, and the useful number is the one you get on the same problem a month from now, cold.
kept in this browser only