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.
class Solution {
public:
bool isOneRow(string s,map<char,int> &temp)
{
int len = s.length();
int flag = 0;
for(int i = 0;i<len;i++)
{
if(i == 0)
flag = temp[toupper(s[i])];
else
{
int m = temp[toupper(s[i])];
if(flag != m)
return false;
}
}
return true;
}
vector<string> findWords(vector<string>& words) {
map<char,int> temp;
temp['A'] = 2;
temp['S'] = 2;
temp['D'] = 2;
temp['F'] = 2;
temp['G'] = 2;
temp['H'] = 2;
temp['J'] = 2;
temp['K'] = 2;
temp['L'] = 2;
temp['Q'] = 1;
temp['W'] = 1;
temp['E'] = 1;
temp['R'] = 1;
temp['T'] = 1;
temp['Y'] = 1;
temp['U'] = 1;
temp['I'] = 1;
temp['O'] = 1;
temp['P'] = 1;
temp['Z'] = 3;
temp['X'] = 3;
temp['C'] = 3;
temp['V'] = 3;
temp['B'] = 3;
temp['N'] = 3;
temp['M'] = 3;
vector<string> result;
vector<string>::iterator iter = words.begin();
for(;iter != words.end();++iter)
{
if(isOneRow(*iter,temp))
result.push_back(*iter);
}
return result;
}
};