C++ 0ms solution


  • 0
    class Solution {
    public:
    
        static const int ROW_TABLE[];
    
        bool inRow(string str) {
            int lastRow = -1;
            for(int i=0; str[i]!=0; i++){
                char c = toupper(str[i]);
                if(lastRow==-1)
                    lastRow = ROW_TABLE[c-'A'];
                else if(lastRow != ROW_TABLE[c-'A'])
                    return false;
            }
            return true;
        }
    
        vector<string> findWords(vector<string>& words) {
            vector<string> returnWords;
            for(int i=0; i<words.size(); i++) 
                if(inRow(words[i])) 
                    returnWords.push_back(words[i]);
            return returnWords;
        }
        
    };
    
    const int Solution::ROW_TABLE[] = {
        1, 2, 2, 1, 0, 1, 1, 1, 0, 1, 1, 1, 2, 2, 0, 0, 0, 0, 1, 0, 0, 2, 0, 2, 0, 2
      /*A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z*/
    };
    

Log in to reply
 

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