Java 1 ms with simple and readable code with comments
\n/*\nIMP Note: If any of the player wins, their count can not match!!  Since X starts first, it\'s always ahead of \'O\'\n*/\nclass Solution {\n    \n    publi
```\n/*\nIMP Note: If any of the player wins, their count can not match!!  Since X starts first, it\'s always ahead of \'O\'\n*/\nclass Solution {\n    \n    public boolean validTicTacToe(String[] board) {\n        \n        // if board is empty it\'s valid\n        if(board.length == 0){\n            return true;\n        }\n        \n        int count_x = 0;\n        int count_o = 0;\n        int empty_count = 0;\n        \n        // check the count for X, O and empty cell\n        for(String s : board){\n            for(char c : s.toCharArray()){\n                if(c == \'X\'){\n                    count_x++;\n                }else if(c == \'O\'){\n                    count_o++;\n                }else{\n                    empty_count++;\n                }\n            }\n        }\n        \n        boolean x_won = checkForWin(board,\'X\');\n        boolean o_won = checkForWin(board,\'O\');\n        \n        if(empty_count == 9){ // if the board is empty  ( 3 X 3 =9)\n            return true;\n        }else if(count_o > count_x){ // count for O can\'t be greater because X starts first\n            return false;\n        }else if(x_won && o_won){ // both can\'t win at the same time\n            return false;\n        }else if(o_won && count_x > count_o){ // if O wins but X tried putting even after O won\n            return false;\n        }\n        else if(x_won  && count_x-count_o == 0){    // X wins but O tried putting even after X won\n            return false;\n        }\n        else if(count_x-count_o == 1){ // X is always 1 step ahead of O\n            return true;\n        }else if(count_x == count_o && empty_count > 0){ // if X and O have same count then there are more than one empty cells in the grid\n            return true;\n        }\n        \n        return false;\n    }\n    \n    /* Method to check if X won or O won */\n    public boolean checkForWin(String[] board, char check_for){\n        char[][] b = new char[3][3];\n        \n        // store this in 3 X 3 matrix called \'b\' for simplicity and better readability \n        for(int r = 0; r < 3; r++){\n            for(int c = 0; c < 3; c++){\n                char[] arr = board[r].toCharArray();\n                b[r][c] = arr[c];\n            }\n        }\n        \n        //check for rows\n        for(int r = 0; r < 3; r++){\n            if(b[r][0] == b[r][1] && b[r][0] == b[r][2] && b[r][0] == check_for){\n                return true;\n            }\n        }\n        \n        // check for cols\n        for(int c = 0; c < 3; c++){\n            if(b[0][c] == b[1][c] && b[0][c] == b[2][c] && b[2][c] == check_for){\n                return true;\n            }\n        }\n        \n        // check for diagonal\n        if(b[0][0] == b[1][1] && b[0][0] == b[2][2] && b[0][0] == check_for){\n            return true;\n        }\n        \n        // check for anti-diagonal\n        if(b[0][2] == b[1][1] && b[2][0] == b[1][1] && b[1][1] == check_for){\n            return true;\n        }\n        \n\t\t//the player didn\'t win\n        return false;\n    }\n    \n}\n```
['Java']