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. I tried writing this but if condition is never being exceuted. I am not able to figure out the reason. Please help

var lengthOfLongestSubstring = function(s) {
 let set = new Set();
    let c =0;
    for(let i =0; i< s.length; i++){
        if(set.has(s[i])){
            set.size =0;
        }
        else {
            console.log(c)
            c++;
        }
    }
    return c;
}; ```

115 thoughts on “Given a string s, find the length of the longest substring without repeating characters”

Leave a Comment