sliding-window · Problem 2 of 2

Longest Substring Without Repeats

medium
Amazon logoAmazon
Google logoGoogle
Meta logoMeta
Bloomberg logoBloomberg

Return the length of the longest substring of s containing no repeated character.

lengthOfLongestSubstring("abcabcbb") -> 3

Your 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
calltypeexpectedresult
lengthOfLongestSubstring("abcabcbb")classic3
lengthOfLongestSubstring("bbbbb")all same1
lengthOfLongestSubstring("pwwkew")mixed3
lengthOfLongestSubstring("")empty0
withheldhiddenwithheld

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