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"] 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.判断给定的单词字母是否只来自键盘上某一行
class Solution { public: void init(int a[]){ a['Q'-65]=a['W'-65]=a['E'-65]=a['R'-65]=a['T'-65]=a['Y'-65]=1; a['U'-65]=a['I'-65]=a['O'-65]=a['P'-65]=1; a['A'-65]=a['S'-65]=a['D'-65]=a['F'-65]=a['G'-65]=a['H'-65]=2; a['J'-65]=a['K'-65]=a['L'-65]=2; a['Z'-65]=a['X'-65]=a['C'-65]=a['V'-65]=a['B'-65]=a['N'-65]=a['M'-65]=3; } int judge(string s,int a[]){ char tmp=toupper(s[0]); int lable=a[tmp-65],i; for(i=1;i<s.length();i++){ tmp=toupper(s[i]); if(a[tmp-65]!=lable){ return 0; } } return 1; } vector<string> findWords(vector<string>& words) { int a[26],i; vector<string>ans; init(a); for(i=0;i<words.size();i++){ if(judge(words[i],a)){ ans.push_back(words[i]); } } return ans; } };