Facebook PixelLongest Substring Without Repeating Characters — Coding Practice
Longest Substring Without Repeating CharactersMedium

Longest Substring Without Repeating Characters

Medium 41.8k35% acceptance
Hash TableStringSliding Window

Given a string s, return the length of the longest substring that contains no repeating characters.

A substring is a contiguous block of characters; it is not the same as a subsequence.

Example 1
Input: s = "abcabcbb"
Output: 3
The longest substring without a repeat is "abc", which has length 3.
Example 2
Input: s = "bbbbb"
Output: 1
Every character is the same, so the best you can do is a single "b".
Example 3
Input: s = "pwwkew"
Output: 3
The answer is "wke". Note "pwke" is a subsequence, not a substring.
Constraints
  • 0 ≤ s.length ≤ 5·10⁴
  • `s` consists of English letters, digits, symbols, and spaces.
Asked atAmazonGoogleMetaMicrosoftBloomberg
JavaScript
Loading editor…
Case 1
"abcabcbb"
expected: 3
Case 2
"bbbbb"
expected: 1
Case 3
"pwwkew"
expected: 3
Case 4
""
expected: 0
Case 5
" "
expected: 1
Case 6
"dvdf"
expected: 3
Case 7
"tmmzuxt"
expected: 5