C++


  • 0
    J
    class Solution {
    public:
        vector<string> findWords(vector<string>& words) {
            vector<string>dict;
            vector<string> res;
            dict.push_back("qwertyuiopQWERTYUIOP");
            dict.push_back("asdfghjklASDDFGHJKL");
            dict.push_back("zxcvbnmZXCVBNM");
            for(auto word:words)
            {
                if(isValidRow(dict,word))
                    res.push_back(word);
            }
            return res;
        }
    private:
        bool isValidRow(vector<string>dict,string word)
        {
            int row = 0;
            for(;row<dict.size();row++)
            {
               int pos = dict[row].find(word[0]);
               if(pos!=std::string::npos) break;
            }
            if(row==dict.size()) return false;
            for(int i =1;i<word.size();i++)
            {
                if(dict[row].find(word[i])==std::string::npos) return false;
            }
            return true;
        }
    };
    

Log in to reply
 

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