my JavaScript solutions


  • 0
    A

    Solution 1:

    var findWords = function(words) {
        var rows = {'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' : 3, 'x' : 3, 'c' : 3, 'v' : 3, 'b' : 3, 'n' : 3, 'm' : 3};
    
        function isOneRow(word) {
            if (word.length < 2) return true;
            word = word.toLowerCase();
            var row = rows[word[0]];
            return word.split('').every(c => rows[c] === row);
        }
        return words.filter(isOneRow);
    };
    

    Regular Express solution:

    var findWords = function(words) {
        return words.filter(function(word) {
            return /^([qwertyuiop]*|[asdfghjkl]*|[zxcvbnm]*)$/.test(word.toLowerCase());
        });
    };
    

Log in to reply
 

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