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:
vector<string> findWords(vector<string>& words) {
string rows[] = {"qwertyuiop", "asdfghjkl", "zxcvbnm"};
vector<string> vs;
for (int i = 0; i < words.size(); i++)
{
string s = words[i];
int r = -1;
int j = 0;
for (j = 0; j < s.size(); j++) {
if (r == -1) {
while (r < 2) {
r++;
if (string::npos != rows[r].find(tolower(s[j]))) {
break;
}
}
} else {
if (string::npos == rows[r].find(tolower(s[j]))) {
break;
}
}
}
if (j == s.size()) {
vs.push_back(s);
}
}
return vs;
}
};