a JavaScript solution


  • 0
    /**
     * @param {string} s
     * @return {number}
     */
    var lengthOfLongestSubstring = function(s) {
        var sub=new Map(),
            max=0,
            count=0,
            head=0;
    
        for(var i=0;i<s.length;i++){
            count++;
            if(!sub.has(s[i])||sub.get(s[i])<head){
                sub.set(s[i],i);
    
            }
            else{
                count=count-sub.get(s[i])+head-1;
                head=sub.get(s[i])+1;
                sub.set(s[i],i);
            }
            if(count>max){
                max=count;
            }
    
        }
        return max;
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.