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.
    500. Keyboard Row - 图1
    Example: Input: [“Hello”, “Alaska”, “Dad”, “Peace”] Output: [“Alaska”, “Dad”]Note:

    1. You may use one character in the keyboard more than once.
    2. You may assume the input string will only contain letters of alphabet.

    Runtime: 0 ms, faster than 100.00% of C++ online submissions for Keyboard Row.

    1. class Solution {
    2. public:
    3. vector<string> findWords(vector<string> &words) {
    4. vector<unordered_set<char>> dict = {
    5. {'q', 'Q', 'w', 'W', 'e', 'E', 'r', 'R', 't', 'T', 'y', 'Y', 'u', 'U', 'i', 'I', 'o', 'O', 'p', 'P'},
    6. {'a', 'A', 's', 'S', 'd', 'D', 'f', 'F', 'g', 'G', 'h', 'H', 'j', 'J', 'k', 'K', 'l', 'L'},
    7. {'z', 'Z', 'x', 'X', 'c', 'C', 'v', 'V', 'b', 'B', 'n', 'N', 'm', 'M'}};
    8. vector<string> res;
    9. for (auto &word : words) {
    10. vector<bool> d(3, true);
    11. for (auto &ch : word) {
    12. for (int i = 0; i < 3; ++i) {
    13. if (d[i] && dict[i].find(ch) == dict[i].end()) {
    14. d[i] = false;
    15. }
    16. }
    17. }
    18. if (d[0] || d[1] || d[2]) {
    19. res.push_back(word);
    20. }
    21. }
    22. return res;
    23. }
    24. };