Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Longest Substring Without Repeating Characters

Sliding Windowmedium

Implement `lengthOfLongestSubstring(s)` returning the length of the longest substring of `s` with no repeating characters.

Sample tests

Input: lengthOfLongestSubstring("abcabcbb")
Output: 3
Input: lengthOfLongestSubstring("bbbbb")
Output: 1

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Resetting the start too far back when a repeat is outside the current window.

Learning resources

  • MDN: String
Approach & explanation (try first)

A variable window with last-seen indices finds the longest unique substring in O(n).

Loading...
⌘/Ctrl + Enter

Run your code to see results.