Keyboard c++ solution


  • 0
    T

    '''
    class Solution {
    public:
    std::unordered_map<char, int> key_row_map = {
    {'Q', 1}, {'W', 1}, {'E', 1}, {'R', 1}, {'T', 1}, {'Y', 1}, {'U', 1}, {'I', 1}, {'O', 1}, {'P', 1},
    {'A', 2}, {'S', 2}, {'D', 2}, {'F', 2}, {'G', 2}, {'H', 2}, {'J', 2}, {'K', 2}, {'L', 2},
    {'Z', 3}, {'X', 3}, {'C', 3}, {'V', 3}, {'B', 3}, {'N', 3}, {'M', 3},
    {'q', 1}, {'w', 1}, {'e', 1}, {'r', 1}, {'t', 1}, {'y', 1}, {'u', 1}, {'i', 1}, {'o', 1}, {'p', 1},
    {'a', 2}, {'s', 2}, {'d', 2}, {'f', 2}, {'g', 2}, {'h', 2}, {'j', 2}, {'k', 2}, {'l', 2},
    {'z', 3}, {'x', 3}, {'c', 3}, {'v', 3}, {'b', 3}, {'n', 3}, {'m', 3},
    };

    vector<string> findWords(vector<string>& words) {
        std::vector<string> result;
        for(auto word: words) {
            int row = 0;
            bool same_row = true;
            for(auto letter: word) {
                auto letter_iter_map = key_row_map.find(letter);
                if(letter_iter_map != key_row_map.end()) {
                    if(!row)
                        row = letter_iter_map->second;
                    else if(row != letter_iter_map->second) {
                        same_row = false;
                        break;
                    }
                }
                else {
                    same_row = false;
                    break;
                }
            }
            if(same_row)
                result.push_back(word);
        }
        return result;
    }
    

    };
    '''


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.