Byte operations solution


  • 0
    R

    Javascript solution using byte operations

    /**
     * @param {string[]} words
     * @return {string[]}
     */
    var findWords = function(words) {
        var keysMap = {'q': 1, 'w': 1, 'e': 1, 'r': 1, 't': 1, 'y': 1, 'u': 1, 'i': 1, 'o': 1, 'p': 1,
            'a': 2, 's': 2, 'd': 2, 'f': 2, 'g': 2, 'h': 2, 'j': 2, 'k': 2, 'l': 2,
            'z': 4, 'x': 4, 'c': 4, 'v': 4, 'b': 4, 'n': 4, 'm': 4
        };
        var res = [],
            equal;
        
        for(var num in words){
            word = words[num].toLowerCase();
            equal = keysMap[word[0]];
            for(var i = 1; i < word.length; i++){
                equal |= keysMap[word[i]];
            }
            if(equal === keysMap[word[0]])
                res.push(words[num]);
        }
    
        return res;
    };

Log in to reply
 

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