jzhangnu / Leetcode-JS-Solutions

:tropical_drink: Leetcode solutions using JavaScript.
52 stars 7 forks source link

79. Word Search #112

Open jzhangnu opened 7 years ago

jzhangnu commented 7 years ago
var exist = function(board, word) {
    board = board.reduce(function(counted, e){
        return counted.concat(e);
    })

    var i = 0,
        hash = {};

    board.forEach(function(e){
        if(hash[e])hash[e]++;
        else hash[e] = 1;
    })

    word.split('').forEach(function(e){
        if(hash[e])hash[e]--;
        else return false
    })

    return true
};
Samueloyeks commented 4 years ago

Oh my!!! This is so simple and effective...Please can you explain this, I have been struggling with this for a while now