javascript solution without hashtable

  • 0

    Loop trough the ransom note, make use of indexOf to track if the magazine contains the character in ransom note. If result is found, delete that in the magazine.

     * @param {string} ransomNote
     * @param {string} magazine
     * @return {boolean}
    var canConstruct = function(ransomNote, magazine) {
        for(let i = 0; i < ransomNote.length; i++){
            let index = magazine.indexOf(ransomNote[i]);
            if(index >= 0){
                magazine = magazine.slice(0,index)+magazine.slice(index+1);
                return false;
        return true;

Log in to reply

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