Javascript solution


  • 0
    S
    /**
     * @param {string} ransomNote
     * @param {string} magazine
     * @return {boolean}
     */
    var canConstruct = function(ransomNote, magazine) {
        if(ransomNote.length > magazine.length) {
            return false;
        }
        for(let i = ransomNote.length - 1; i >= 0; i--) {
            let index = magazine.indexOf(ransomNote[i]);
            if(index === -1) {
                return false;
            }
            else {
                magazine = magazine.replace(magazine[index], '');
            }
        }
        return true;
    };
    

Log in to reply
 

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