Easy C++ solution using find_first_not_of()


  • 0
    R

    find_first_not_of(string str1) returns the first position of occurrence of characters NOT in str1, or string::npos if none found.

    class Solution {
    public:
        vector<string> findWords(vector<string>& words) {
            string line1 = "qwertyuiopQWERTYUIOP";
            string line2 = "asdfghjklASDFGHJKL";
            string line3 = "zxcvbnmZXCVBNM";
            vector<string> results = {};
            
            for(int i=0; i<words.size(); i++){
                bool inL1 = words[i].find_first_not_of(line1) == string::npos;
                bool inL2 = words[i].find_first_not_of(line2) == string::npos;
                bool inL3 = words[i].find_first_not_of(line3) == string::npos;
                
                if(inL1 || inL2 || inL3){
                    results.push_back(words[i]);
                }
            }
            
            return results;
        }
    };
    

Log in to reply
 

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