题目描述

Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.

keyboard

Example 1:

Input: ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]

Note:

  1. You may use one character in the keyboard more than once.
  2. You may assume the input string will only contain letters of alphabet.

解法一

思路

这题非常简单,不必作太多解释了。只需要将相同行的字母都映射成相同的数字,然后对每个单词测试是不是所有的字母都对应相同的数字;这里直接拿第一个字母对应的数字作为基准,检查后面的所有字母是否与第一个数字相同即可。

Python

class Solution(object):
    def findWords(self, words):
        """
        :type words: List[str]
        :rtype: List[str]
        """
        pattern = {}
        for letter in 'qwertyuiop':
            pattern[letter] = 1
        for letter in 'asdfghjkl':
            pattern[letter] = 2
        for letter in 'zxcvbnm':
            pattern[letter] = 3
        result = []
        for word in words:
            word_lower = word.lower()
            category = pattern[word_lower[0]]
            is_same_category = True
            for letter in word_lower:
                if pattern[letter] != category:
                    is_same_category = False
                    break
            if is_same_category:
                result.append(word)
        return result

Java

public class Solution {
    public String[] findWords(String[] words) {
        Map<Character, Integer> pattern = new HashMap<>();
        for (char letter : "qwertyuiop".toCharArray()) {
            pattern.put(letter, 1);
        }
        for (char letter : "asdfghjkl".toCharArray()) {
            pattern.put(letter, 2);
        }
        for (char letter : "zxcvbnm".toCharArray()) {
            pattern.put(letter, 3);
        }
        List<String> resultList = new ArrayList<>();
        for (String word : words) {
            String wordLower = word.toLowerCase();
            int category = pattern.get(wordLower.charAt(0));
            boolean isSameCategory = true;
            for (char letter : wordLower.toCharArray()) {
                if (pattern.get(letter) != category) {
                    isSameCategory = false;
                    break;
                }
            }
            if (isSameCategory) {
                resultList.add(word);
            }
        }
        return resultList.toArray(new String[resultList.size()]);
    }
}

C++

class Solution {
public:
    vector <string> findWords(vector <string> &words) {
        class Utils {
        public:
            char easytolower(char in) {
                if (in <= 'Z' && in >= 'A')
                    return in - ('Z' - 'z');
                return in;
            }
        };
        unordered_map<char, int> pattern;
        for (auto letter:"qwertyuiop") {
            pattern[letter] = 1;
        }
        for (auto letter:"asdfghjkl") {
            pattern[letter] = 2;
        }
        for (auto letter:"zxcvbnm") {
            pattern[letter] = 3;
        }
        Utils utils;
        vector <string> result;
        for (auto &word:words) {
            int category = pattern[utils.easytolower(word[0])];
            bool isSameCategory = true;
            for (auto letter:word) {
                if (pattern[utils.easytolower(letter)] != category) {
                    isSameCategory = false;
                    break;
                }
            }
            if (isSameCategory) {
                result.push_back(word);
            }
        }
        return result;
    }
};