class Solution {
public:
vector<string> findWords(vector<string>& words) {
vector<string> res; //定義一個string類型的vector
unordered_set<char> a{'q','w','e','r','t','y','u','i','o','p','Q','W','E','R','T','Y','U','I','O','P'};
unordered_set<char> b{'A','S','D','F','G','H','J','K','L','a','s','d','f','g','h','j','k','l'};
unordered_set<char>
c{'z','x','c','v','b','n','m','Z','X','C','V','B','N','M'};
//a,b,c爲無序集合,分別包含鍵盤每行的字母大小寫;
for(int i=0;i!=words.size();i++){ //對words中的每一個字符串進行循環
int x=0;
int y=0;
int z=0;//每次處理完一個字符串均要初始化xyz的值,防止後面的判斷出錯;
for(int j=0;j!=words[i].size();j++) //對每個字符串中的字符進行循環
{
if(a.count(words[i][j])) x=1;
else if(b.count(words[i][j])) y=1;
else if(c.count(words[i][j])) z=1; //unordered.count(i)是返回i出現的次數, 若 出現某一行鍵盤的字母,則設置x/y/z=1;
if(x+y+z>1) break; //若是出現超過一行的字母,則跳出,進行下一個字 符串的判斷;
}
if(x+y+z==1){res.push_back(words[i]);} //若該字符串僅含一行鍵盤的字母,則將其添 加至res中;
}
return res;
}
};