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.
解题思路:将三行存入一个二维数组里,用vector做二维数组。设置flag来判断是哪一行,然后判断字符串是否在同一行
class Solution { public: vector<string> findWords(vector<string> &words) { vector<unordered_set<char>> dict = { {'q', 'Q', 'w', 'W', 'e', 'E', 'r', 'R', 't', 'T', 'y', 'Y', 'u', 'U', 'i', 'I', 'o', 'O', 'p', 'P'}, {'a', 'A', 's', 'S', 'd', 'D', 'f', 'F', 'g', 'G', 'h', 'H', 'j', 'J', 'k', 'K', 'l', 'L'}, {'z', 'Z', 'x', 'X', 'c', 'C', 'v', 'V', 'b', 'B', 'n', 'N', 'm', 'M'}}; vector<string> res; for (auto &word : words) { vector<bool> d(3, true); for (auto &ch : word) for (int i = 0; i < 3; ++i) if (d[i] && dict[i].find(ch) == dict[i].end()) d[i] = false; if (d[0] || d[1] || d[2]) res.push_back(word); } return res; } };