Javascript solution .But Time limited exceeded.How to improve it?


  • 0
    M

    var canConstruct = function(ransomNote, magazine) {
    if(ransomNote.length>magazine.length){
    return false;
    }

    var a=new Array(26),b=new Array(26);
    for(var i=0;i<ransomNote.length;i++){
        a[ransomNote.charAt(i)-'a'] ++;
        
    }
     for(var j=0;j<magazine.length;i++){
        b[magazine.charAt(j)-'a'] ++;
        
    }
    
    for(var h=0;i<ransomNote.length;h++)
    {
        if(a[h]>b[h])
        {
            return false;
        }
        
    }
    
    return true;
    

    };


Log in to reply
 

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