给定一个单词列表,返回可以在美国键盘的一行上使用字母表键入的单词,如下图所示。
样例
输入: ["Hello", "Alaska", "Dad", "Peace"]
输出: ["Alaska", "Dad"]
注意事项
您可以多次使用键盘中的一个字符。
您可以假设输入字符串仅包含字母表的字母。
class Solution {
public:
/**
* @param words: a list of strings
* @return: return a list of strings
*/
vector<string> findWords(vector<string> &words) {
// write your code here
vector<string> ret;
set<char> myset1;
set<char> myset2;
set<char> myset3;
myset1.insert('Q');
myset1.insert('W');
myset1.insert('E');
myset1.insert('R');
myset1.insert('T');
myset1.insert('Y');
myset1.insert('U');
myset1.insert('I');
myset1.insert('O');
myset1.insert('P');
myset2.insert('A');
myset2.insert('S');
myset2.insert('D');
myset2.insert('F');
myset2.insert('G');
myset2.insert('H');
myset2.insert('J');
myset2.insert('K');
myset2.insert('L');
myset3.insert('Z');
myset3.insert('X');
myset3.insert('C');
myset3.insert('V');
myset3.insert('B');
myset3.insert('N');
myset3.insert('M');
myset1.insert('q');
myset1.insert('w');
myset1.insert('e');
myset1.insert('r');
myset1.insert('t');
myset1.insert('y');
myset1.insert('u');
myset1.insert('i');
myset1.insert('o');
myset1.insert('p');
myset2.insert('a');
myset2.insert('s');
myset2.insert('d');
myset2.insert('f');
myset2.insert('g');
myset2.insert('h');
myset2.insert('j');
myset2.insert('k');
myset2.insert('l');
myset3.insert('z');
myset3.insert('x');
myset3.insert('c');
myset3.insert('v');
myset3.insert('b');
myset3.insert('n');
myset3.insert('m');
for(int i = 0; i < words.size(); i++)
{
char ch = words[i][0];
set<char> myset;
if(myset1.find(ch) != myset1.end())
{
myset = myset1;
}
else if(myset2.find(ch) != myset2.end())
{
myset = myset2;
}
else if(myset3.find(ch) != myset3.end())
{
myset = myset3;
}
int j = 1;
for( ;j < words[i].size(); j++)
{
if(myset.find(words[i][j]) == myset.end())
{
break;
}
}
if(j == words[i].size())
{
ret.push_back(words[i]);
}
}
return ret;
}
};