Easy C++ solution


  • 0
    class Solution {
    public:
        vector<string> findWords(vector<string>& words) {
            vector<string> res;
            if(words.size()==0) return res;
            string row1 = "qwertyuiop", row2 = "asdfghjkl", row3 = "zxcvbnm";
            for(int i=0; i<words.size(); ++i){
                string s = words[i];
                int count1 = 0, count2 = 0, count3 = 0;
                for(int j=0; j<s.size(); ++j) {if(row1.find(tolower(s[j]))!=string::npos) count1++;}
                for(int j=0; j<s.size(); ++j) {if(row2.find(tolower(s[j]))!=string::npos) count2++;}
                for(int j=0; j<s.size(); ++j) {if(row3.find(tolower(s[j]))!=string::npos) count3++;}
                if(count1 == s.size() || count2 == s.size() || count3 == s.size()) res.push_back(s);
            }
            return res;
        }
    };
    

Log in to reply
 

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