Longest Substring Without Repeating Characters

MediumSliding Window

Given a string s, return the length of the longest substring (a contiguous stretch of characters) in which no character appears more than once.

Every character counts, including spaces, digits and punctuation. Upper- and lowercase letters are different characters.

Constraints

  • 0 <= s.length <= 5 * 10^4
  • s contains English letters, digits, symbols and spaces.

Examples

Example 1

Input: s = "abcdbefg"
Output: 6

"cdbefg" (indices 2 to 7) has no repeats. Any longer substring would contain both b's.

Example 2

Input: s = "zzzz"
Output: 1

Every substring longer than one character repeats z.

Example 3

Input: s = ""
Output: 0

The empty string has no characters, so the answer is 0.

JavaScript you'll use: Map and Set Strings and characters

/**
 * @param {string} s
 * @return {number}
 */
function lengthOfLongestSubstring(s) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.