Really Simple and Easy to Understand C++ Solution


  • 0
    L
    class Solution {
    public:
        vector<string> findWords(vector<string>& words) {
            // for convenience hard code both upper/lower case
            string levels[] = {"QWERTYUIOPqwertyuiop", "ASDFGHJKLasdfghjkl", "ZXCVBNMzxcvbnm"};
            vector<string> out_arr;
            
            for (int i = 0; i < words.size(); ++i) {
                for (int k = 0; k < sizeof(levels)/sizeof(levels[0]); ++k) {
                    bool change_row = false;
                    for (int j = 0; j < words[i].length(); ++j) {
                        if (levels[k].find(words[i][j]) == string::npos) {
                            change_row = true;
                        }
                    }
                    if (!change_row) {
                        out_arr.push_back(words[i]);
                    } else {
                        continue; // next row for k
                    }
                }       
            }
            
            return out_arr;
        }
    };
    

Log in to reply
 

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