# 3. Longest Substring Without Repeating Characters

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

Input: s = "abcabcbb"Output: 3Explanation: The answer is "abc", with the length of 3.Input: s = "pwwkew"Output: 3Explanation: 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 question can be solved by sliding window.

• When should I move the left pointer?
• When should I move the right pointer?

Solution:

/** * @param {string} s * @return {number} */var lengthOfLongestSubstring = function (s) {  let left = 0;  let right = 0;  const set = new Set();  let result = 0;  while (right < s.length) {    while (set.has(s.charAt(right))) {      set.delete(s.charAt(left)); // move left boundary      left++;    }    set.add(s.charAt(right));    right++;    result = Math.max(result, set.size);  }  return result;};console.log(lengthOfLongestSubstring("pwwkew")); // 3console.log(lengthOfLongestSubstring("abcabcbb")); // 3