C++ Solution using ASCII


  • 0
    Y
    class Solution {
    public:
        vector<string> findWords(vector<string>& words) {
            int rank[100];
            int row = 0;
            vector<string> results;
            fillRank(rank, 100);
            
            for(int i = 0; i < words.size(); i++) {
                bool pass = true;
                for(int j = 0; j < words[i].size(); j++) {
                    if(j == 0) {
                        if(rank[(int)toupper(words[i][j])] > 0 && rank[(int)toupper(words[i][j])] <= 10) {
                            row = 0;
                        } else if(rank[(int)toupper(words[i][j])] > 10 && rank[(int)toupper(words[i][j])] <= 19) {
                            row = 1;
                        } else if(rank[(int)toupper(words[i][j])] >=20 && rank[(int)toupper(words[i][j])] <= 26) {
                            row = 2;
                        }
                    }
                    
                    switch(row) {
                        case 0:
                        pass = (rank[(int)toupper(words[i][j])] <= 10);
                        break;
                        case 1:
                        pass = (rank[(int)toupper(words[i][j])] > 10 && rank[(int)toupper(words[i][j])] <= 19);
                        break;
                        case 2:
                        pass = (rank[(int)toupper(words[i][j])] >= 20);
                        break;
                    }
                    
                    if(!pass)
                        break;
                }
                
                if(pass)
                    results.push_back(words[i]);
            }
            
            return results;
        }
        
        void fillRank(int rank[], int size) {
            rank[(int)'Q'] = 1;
            rank[(int)'W'] = 2;
            rank[(int)'E'] = 3;
            rank[(int)'R'] = 4;
            rank[(int)'T'] = 5;
            rank[(int)'Y'] = 6;
            rank[(int)'U'] = 7;
            rank[(int)'I'] = 8;
            rank[(int)'O'] = 9;
            rank[(int)'P'] = 10;
            rank[(int)'A'] = 11;
            rank[(int)'S'] = 12;
            rank[(int)'D'] = 13;
            rank[(int)'F'] = 14;
            rank[(int)'G'] = 15;
            rank[(int)'H'] = 16;
            rank[(int)'J'] = 17;
            rank[(int)'K'] = 18;
            rank[(int)'L'] = 19;
            rank[(int)'Z'] = 20;
            rank[(int)'X'] = 21;
            rank[(int)'C'] = 22;
            rank[(int)'V'] = 23;
            rank[(int)'B'] = 24;
            rank[(int)'N'] = 25;
            rank[(int)'M'] = 26;
        }
    };
    

Log in to reply
 

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