# 【LeetCode 500】关关的刷题日记27 Keyboard Row

## 题目

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 1: Input: ["Hello", "Alaska", "Dad", "Peace"] Output: ["Alaska", "Dad"] Note: You may use one character in the keyboard more than once. You may assume the input string will only contain letters of alphabet.

## 思路

```lass Solution {public:
vector<string> findWords(vector<string>& words) {
set<char>row1={'q','w','e','r','t','y','u','i','o','p'};
set<char>row2={'a','s','d','f','g','h','j','k','l'};
set<char>row3={'z','x','c','v','b','n','m'};
vector<string>output;
int i, j, k,p;
for( i=0; i<words.size(); i++)
{
for( j=0; j<words[i].size(); j++)
{
if(row1.find(tolower(words[i][j]))==row1.end())
break;
}
for( k=0; k<words[i].size(); k++)
{
if(row2.find(tolower(words[i][k]))==row2.end())
break;
}
for( p=0; p<words[i].size(); p++)
{
if(row3.find(tolower(words[i][p]))==row3.end())
break;
}
int n=words[i].size();
if(j==n || k==n || p==n )
output.push_back(words[i]);
}
return output;
}};```

0 条评论

3737

961

3368

2970

5298

20010

1441

7276

2865

2503