2022/12/24

LeetCode 500. Keyboard Row

問題:判斷文字是否為同一列鍵盤所輸入文字 解法:交集文字與每列鍵盤文字去比較 JavaScript
/**
 * @param {string[]} words
 * @return {string[]}
 */
var findWords = function (words) {
    const temp = ["qwertyuiop".split(""), "asdfghjkl".split(""), "zxcvbnm".split("")];
    if (words.length < 1 || words.length > 20) {
        return;
    }

    let result = [];
    for (let i = 0; i < words.length; i++) {
        const wordArray = words[i].toLowerCase().split("");
        const len = words[i].length;
        if (wordArray.filter(s => temp[0].includes(s)).length !== len &&
            wordArray.filter(s => temp[1].includes(s)).length !== len &&
            wordArray.filter(s => temp[2].includes(s)).length !== len) {
            continue;
        }
        result.push(words[i]);
    }

    return result;
};