给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词。键盘如下图所示。
示例:
输入: ["Hello", "Alaska", "Dad", "Peace"]
输出: ["Alaska", "Dad"]
注意:
- 你可以重复使用键盘上同一字符。
你可以假设输入的字符串将只包含字母。
class Solution { public: vector<string> findWords(vector<string>& words) { vector<string> res; if(words.size() == 0){ return res; } unordered_map<char, int> hashMap; char first[] = {'Q', 'W','E','R','T','Y','U', 'I','O','P'}; char secon[] = {'A', 'S','D','F','G','H','J', 'K','L'}; char third[] = {'Z', 'X','C','V','B','N','M'}; for(int i=0;i<10;i++){ hashMap[first[i]] = 1; } for(int i=0;i<9;i++){ hashMap[secon[i]] = 2; } for(int i=0;i<7;i++){ hashMap[third[i]] = 3; } for(int i = 0; i<words.size();i++){ int referen = hashMap[toupper(words[i][0])]; cout<<referen<<endl; bool flag = true; for(int j = 1; j<words[i].size();j++){ if(referen != hashMap[toupper(words[i][j])]){ flag = false; } } if(flag == true){ res.push_back(words[i]); } } return res; } };