C++ solution with map


  • 0
    G
    class Solution {
    public:
        vector<string> findWords(vector<string>& words) {
            string rows[3] = {"qwertyuiopQWERTYUIOP", "asdfghjklASDFGHJKL", "zxcvbnmZXCVBNM"};
            map<char, int> alpha;
            for (int i = 0; i < 3; i++)
                for (int j = 0; j < rows[i].length(); j++)
                    alpha[rows[i][j]] = i;
            
            vector<string> newWords;
            for (auto str : words) {
                bool flag = true;
                int row = alpha[str[0]];
                for (int j = 1; j < str.length(); j++)
                    if (row != alpha[str[j]]) {
                        flag = false;
                        break;
                    }
                if (flag)
                    newWords.push_back(str);
            }
            return newWords;
                
        }
    };
    

Log in to reply
 

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