Brief C++


  • 0
    M
    vector<string> findWords(vector<string>& words) {
        static const struct item {
            bool mask[128];
    
            item(const char* str) : mask{} {
                for (auto ptr = str; *ptr; ++ptr) mask[*ptr] = true;
            }
    
            bool fits(const string& w) const {
                return count_if(begin(w), end(w), [this](char ch) { return mask[::tolower(ch)]; }) == w.size();
            }
        } rows[] = { { "qwertyuiop"}, { "asdfghjkl"}, { "zxcvbnm"} };
    
        auto test = [&](const string& word) {
            return any_of(begin(rows), end(rows), [&](const item& row) { return row.fits(word); });
        };
    
        words.erase(partition(begin(words), end(words), test), end(words));
        return words;
    }
    

Log in to reply
 

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