JavaScript Solution O(N)


  • 0
    G
    var canConstruct = function(ransomNote, magazine) {
        var storage = {};
        
        for (var i = 0; i < magazine.length; i++) {
            var letter = magazine[i];
            
            storage[letter] = storage[letter] || 0;
            storage[letter]++;
        }
        
        
        for (var i = 0; i < ransomNote.length; i++) {
            var letter = ransomNote[i];
            
            if (!storage[letter]) {
                return false;
            }
            
            storage[letter]--;
        }
        
        return true;
    };
    

Log in to reply
 

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