c++ solution


  • 0
    Q
    class Solution {
    public:
        vector<string> findWords(vector<string>& words) {
            unordered_set<char> row1 {'q', 'w', 'e', 'r', 't', 'y','u', 'i', 'o', 'p'};
            unordered_set<char> row2 {'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'}; 
            unordered_set<char> row3 { 'z', 'x', 'c', 'v', 'b' ,'n', 'm'};
            
            vector<string> res;
            for(auto it:words){
                int r1 = 0, r2 = 0, r3 = 0,t=0;
                for(auto a:it){
                    char c = tolower(a);
                    if(row1.find(c) != row1.end())
                        r1++;
                    else if(row2.find(c) != row2.end())
                        r2++;
                    else if(row3.find(c) != row3.end())
                        r3++;
                    
                }
                if(r1+r2 == 0 || r2+r3 == 0 || r1+r3==0)
                    res.push_back(it);
            }
            return res;
        }
    };
    

Log in to reply
 

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