clean and fast solution with a little extra pre-processing. Easy to understand
Instead of  counting \'X\' , \'O\', \' \'\nwe represent these 3 states as 1, -1 and 0.\n\nWe go over board once first, as pre-processing. \nThis sort of transla
Instead of  counting \'X\' , \'O\', \' \'\nwe represent these 3 states as 1, -1 and 0.\n\nWe go over board once first, as pre-processing. \nThis sort of translate board into a 1, -1, and 0 bitmask.\n\nThen rules becomes simple\n1. total sum of steps can only be 1 or 0. (As in 1 goes first, then goes -1)\n2. any row/coln/diag sums to 3/-3 means 1 or -1 wins. (i.e. 111, -1 -1 -1)\n3. last move player has a chance to win or not win, i.e. the other player(non-last move player) cannot win\n\nWe can see after the pre-processing translation, solution becomes much clean and easy to understand. (with a a little space overhead to store translated board)\nO(size of board) runtime [up to beat 100% in C++]\nO(size of board) space\n\n```\nclass Solution {\npublic:\n    bool validTicTacToe(vector<string>& board) {\n        //M1: use 1 as X, -1 as O. \n        //check total sum, check invalid wins from non-last move player\n        int b[3][3] = {};\n        int totalSum = 0;\n        \n        for (int i = 0; i < 3; ++i) {\n            for (int j = 0; j < 3; ++j) {\n                if (board[i][j] == \'X\') {b[i][j] = 1; totalSum += 1;}\n                else if (board[i][j] == \'O\') {b[i][j] = -1; totalSum -= 1;}\n            }            \n        }\n        \n        if (totalSum == 0) {\n            //O plays last, X cant win\n            //check rows/colns\n            for (int i = 0; i < 3; ++i) {\n                if (b[i][0] + b[i][1] + b[i][2] == 3 || \n                    b[0][i] + b[1][i] + b[2][i] == 3) return false;\n            }\n            \n            //check diag\n            int diag1 = 0, diag2 = 0;\n            for (int i = 0; i < 3; ++i) {\n                diag1 += b[i][i];\n                diag2 += b[i][2 - i];\n            }\n            if (diag1 == 3 || diag2 == 3) return false;\n                \n            //valid\n            return true;\n        } else if (totalSum == 1) {\n            //X plays last, O cant win. Repeat last if \n            //check rows/colns\n            for (int i = 0; i < 3; ++i) {\n                if (b[i][0] + b[i][1] + b[i][2] == -3 || \n                    b[0][i] + b[1][i] + b[2][i] == -3) return false;\n            }\n            \n            //check diag\n            int diag1 = 0, diag2 = 0;\n            for (int i = 0; i < 3; ++i) {\n                diag1 += b[i][i];\n                diag2 += b[i][2 - i];\n            }\n            if (diag1 == -3 || diag2 == -3) return false;\n                \n            //valid\n            return true; \n        } else return false; //invalid total steps\n        \n    }\n};\n```
['C']