Longest Substring Without Repeating Characters

Medium 33.5%

Given a string s, find the length of the longest substring without duplicate characters.

Example 1:

Input: s = "abcabcbb"

Output: 3

Explanation: The answer is "abc", with the length of 3. Note that "bca" and "cab" are also correct answers.

Example 2:

Input: s = "bbbbb"

Output: 1

Explanation: The answer is "b", with the length of 1.

Example 3:

Input: s = "pwwkew"

Output: 3

Explanation: The answer is "wke", with the length of 3. Notice that the answer must be a substring; "pwke" is a subsequence and not a substring.

Note:

This environment runs your code for a single testcase at a time. Print only the final output (an integer) so it is compared with the expected result. For quick testing use a print/console output in your chosen language.

  • Python: print(lengthOfLongestSubstring("abcabcbb"))
  • JavaScript: console.log(lengthOfLongestSubstring("abcabcbb"))
  • C++: cout << result << endl;
  • Java: System.out.println(result);

Your Submissions

No submissions yet.

Discuss

Share approaches (sliding-window, index map), analyze complexity, and discuss edge cases.

Test Cases

Test Case 1
Test Case 2
Test Case 3