Leetcode - 3. Longest Substring Without Repeating Characters

Javascript Code /** * @param {string} s * @return {number} */ var lengthOfLongestSubstring = function (s) { const mySet = new Set(); let left = 0; let maxLength = 0; for (let right = 0; right

Mar 3, 2025 - 07:38
 0
Leetcode - 3. Longest Substring Without Repeating Characters

Javascript Code

/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLongestSubstring = function (s) {
    const mySet = new Set();
    let left = 0;
    let maxLength = 0;
    for (let right = 0; right < s.length; right++) {
        while (mySet.has(s[right])) {
            mySet.delete(s[left]);
            left++;
        }
        mySet.add(s[right]);
        maxLength = Math.max(maxLength, mySet.size);
    }
    return maxLength
};