my easy c++ solution


  • 0
    class Solution {
    public:
        vector<string> findWords(vector<string>& words) {
            string first_row = "qwertyuiopQWERTYUIOP";
            string second_row = "asdfghjklASDFGHJKL";
            string third_row = "zxcvbnmZXCVBNM";
            vector<string> res = {};
            
            for(const string word : words){
                bool in_first_row,in_second_row,in_third_row;
                in_first_row = in_second_row = in_third_row = false;
                
                for(const char alpha : word){
                    if(first_row.find(alpha) != string::npos)
                        in_first_row = true;
                    else if(second_row.find(alpha) != string::npos)
                        in_second_row = true;
                    else
                        in_third_row = true;
                }
                bool in_one_row = (in_first_row + in_second_row + in_third_row) == 1;
                if(in_one_row) res.push_back(word);
            }
            return res;
        }
    };
    

Log in to reply
 

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