My javascript solution to this problem


  • 2
    J
    /**
     * @param {string} s
     * @return {number}
     */
    var lengthOfLongestSubstring = function(s) {
        var res = {};
        var i = 0;
        var anchor = 0;
        var len = 0;
        var templen = 0;
    
        for(i = 0; i < s.length; i++){
            if(res[s[i]] === undefined){
                res[s[i]] = i;
            }
            else{
                if(res[s[i]] >= anchor){
                    templen = i - anchor;
                    len = len > templen ? len : templen;
                    anchor = res[s[i]] + 1;                
                }
                res[s[i]] = i;
            }
        }
        templen = i - anchor;
        len = len > templen ? len : templen;
        return len;
    };

  • 0
    D

    useful answer!


Log in to reply
 

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