sliding-window · Problem 2 of 2
Longest Substring Without Repeats
medium
Amazon
Google
Meta
Bloomberg
Return the length of the longest substring of s containing no repeated character.
lengthOfLongestSubstring("abcabcbb") -> 3Your solution
Runs your code and animates it without grading anything. Change the input to see what it does on a case the tests do not cover.
Running is free — Submit is what records it. Or press ⌘↩
Tests
5 cases, 1 hidden| call | type | expected | result |
|---|---|---|---|
| lengthOfLongestSubstring("abcabcbb") | classic | 3 | — |
| lengthOfLongestSubstring("bbbbb") | all same | 1 | — |
| lengthOfLongestSubstring("pwwkew") | mixed | 3 | — |
| lengthOfLongestSubstring("") | empty | 0 | — |
| withheld | hidden | withheld | — |
Hidden cases run too — their inputs aren't listed here, so aim for a general solution rather than one fitted to the cases above.
Complexity
- target time
- O(n)
- target space
- O(k) for k distinct characters
Variable-size window. Each index is visited by the right edge once and the left edge at most once, so it stays linear despite the nested feel.
Hints
Stuck? Hints open one at a time, each giving a little more away.
3 hints left