# 500. Keyboard Row 键盘行

@TOC

## # 题目描述

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.

Example :

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

## # 解题方法

### # 暴力解

``````public class Solution {
public String[] findWords(String[] words) {
char []arr1 = new char[]{'q','w','e','r','t','y','u','i','o','p','Q','W','E','R','T','Y','U','I','O','P'};
char []arr2 = new char[]{'a','s','d','f','g','h','j','k','l','A','S','D','F','G','H','J','K','L'};
char []arr3 = new char[]{'z','x','c','v','b','n','m','Z','X','C','V','B','N','M'};
List<String> ans = new ArrayList<String>();
for(String word: words){
int count1 = 0, count2 = 0, count3 = 0;
for(int i =0; i < word.length(); i++){
for(int j =0; j < arr1.length; j++){
if(word.charAt(i) == arr1[j]){
count1++;
}
}
for(int j =0; j < arr2.length; j++){
if(word.charAt(i) == arr2[j]){
count2++;
}
}
for(int j =0; j < arr3.length; j++){
if(word.charAt(i) == arr3[j]){
count3++;
}
}
}
if((count1 != 0 && count2 == 0 && count3 == 0)
||(count1 == 0 && count2 != 0 && count3 == 0)
||(count1 == 0 && count2 == 0 && count3 != 0)){
}
}
for(int i =0; i < ans.size(); i ++){
}
}
}
``````

### # 字典 + set

``````class Solution:
def findWords(self, words):
"""
:type words: List[str]
:rtype: List[str]
"""
rowdict = {}
for c in "qwertyuiopQWERTYUIOP":
rowdict[c] = 1
for c in "asdfghjklASDFGHJKL":
rowdict[c] = 2
for c in "zxcvbnmZXCVBNM":
rowdict[c] = 3
res = []
for word in words:
if len(set(rowdict[c] for c in word)) == 1:
res.append(word)
return res
``````

## # 日期

2017 年 4 月 2 日 2018 年 11 月 6 日 —— 腰酸背痛要废了