Really Stupid 0ms C++ Solution


  • 0

    just run a check on every char in every word, if one char falls, break and check the next word.

    class Solution {
    public:
        vector<string> findWords(vector<string>& words) {
            string row1 = "qwertyuiop";
            string row2 = "asdfghjkl";
            string row3 = "zxcvbnm";
            
            vector<string> ret;
            
            for (auto& word : words) {
                int cat = 0;
                for(auto& c: word) {
                    char ch = tolower(c);
                    switch(cat) {
                        case 0:
                            if (row1.find(ch) != string::npos) cat = 1;
                            else if (row2.find(ch) != string::npos) cat = 2;
                            else if (row3.find(ch) != string::npos) cat = 3;
                            else cat = -1;
                            break;
                        case 1:
                            if (row1.find(ch) == string::npos) cat = -1;                       
                            break;
    
                        case 2:
                            if (row2.find(ch) == string::npos) cat = -1;
                            break;
    
                        case 3:
                            if (row3.find(ch) == string::npos) cat = -1;
                            break;
    
                        default:
                            cout << "problem";
                            cat = -2;
                            break;
                    }
                    // stop this word if one of the ch fails
                    if (cat == -1) {
                        break;
                    }
                    
                }
                // push the results
                if (cat > 0) {
                    ret.push_back(word);
                }
                
            }
            
            return ret;
        }
    };
    

Log in to reply
 

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