Javascript TLE


  • 0
    R

    Hello, I try many times to Improve my algorithm, but it still get TLE problem.

    Does anyone could help me? pls...

    /**
     * @param {string} s
     * @return {number}
     */
    var lengthOfLongestSubstring = function(s) {
        var sameChar= [];
        var maxStr  = -1;
        var start   = 0;
        for(var i=0; i<s.length; i++){
            if(sameChar[s[i]]!==undefined){
                var subLen = i-start;
                if(subLen>maxStr){ maxStr = subLen;}
                i = sameChar[s[i]];
                start = i+1;
                sameChar= {};
            }else{
                sameChar[s[i]] = i;
            }
        }
        if(s.length-start > maxStr){maxStr=s.length-start;}
        
        return maxStr;
    };
    

Log in to reply
 

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