This website uses cookies to enhance the user experience

Longest Substring Without Repeating Characters

Difficulty: 💪🏽 Medium

Problem Statement

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

Example
Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
Constraints
  • 0 <= s.length <= 5 * 10^4
  • s consists of English letters, digits, symbols and spaces.

Expected Challenge Output

When the function lengthOfLongestSubstring is called with the given example inputs, the expected outputs are:

  • For the input "abcabcbb", the output is 3.
  • For the input "bbbbb", the output is 1.
  • For the input "pwwkew", the output is 3.

These outputs represent the length of the longest substrings without repeating characters in the provided strings.

let s1 = "abcabcbb";
console.log(lengthOfLongestSubstring(s1)); // Output: 3

let s2 = "bbbbb";
console.log(lengthOfLongestSubstring(s2)); // Output: 1

let s3 = "pwwkew";
console.log(lengthOfLongestSubstring(s3)); // Output: 3
function lengthOfLongestSubstring(s) {
// YOUR SOLUTION HERE
}

Memory: 0

CPU: 0