Skip to content

Latest commit

 

History

History
32 lines (29 loc) · 970 Bytes

File metadata and controls

32 lines (29 loc) · 970 Bytes

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

Screen Shot 2021-09-23 at 8 13 14 PM

/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLongestSubstring = function(s) {
    const set = new Set();
    //initial longest substring is 0
    let longest = 0;
    let slow = 0;
    //fast pointer traverse the string
    for(let fast = 0; fast < s.length; fast++) {
        //if the set already has that letter
        while(set.has(s[fast])) {
            //then remove that letter
            set.delete(s[slow]);
            //check the next letter
            slow++;
        }
        //add current into set
        set.add(s[fast]);
        //compare current length with longest length
        longest = Math.max(longest, fast - slow + 1);
    }
    return longest
};