Java -- Explained -- 100% runtime
There are 4 conditions that make an invalid board:\n1) since X starts it must either have the same number of moves as O or exactly one more\n2) both X and O can
There are 4 conditions that make an invalid board:\n1) since X starts it must either have the same number of moves as O or exactly one more\n2) both X and O can\'t win, since if one of them wins the game stops\n3) If X wins, it will always have played on more move than O (since it starts)\n4) If O wins, it must have played the same number of times as O\n\nIf all of these conditions succeed, then we have a successful board, if any of them break our board is broken.\n\n```\nclass Solution {\n    \n    int numX;\n    int numO;\n    \n    public boolean validTicTacToe(String[] board) {\n        // can\'t be two winners AND X has as many spots as O or 1 more max\n        \n        // numbers don\'t add up\n        if (!numXPossible(board))\n            return false;\n        \n        boolean winnerO = checkForWin(board, \'O\');\n        boolean winnerX = checkForWin(board, \'X\');\n        \n        // can\'t both be winners\n        if (winnerO && winnerX)\n            return false;\n        \n        // if X won, it must have had an extra move\n        if (winnerX && this.numX - this.numO != 1)\n            return false;\n        \n        // if O won, the moves must be equal\n        if (winnerO && this.numX - this.numO != 0)\n            return false;\n        \n        return true;\n    }\n    \n    // true indicates the number of Xs to Os is possible\n    public boolean numXPossible(String[] board) {\n        \n        this.numX = 0;\n        this.numO = 0;\n        \n        for (String ln : board) {\n            for (int i = 0; i < 3; i++) {\n                if (ln.charAt(i) == \'X\')\n                    this.numX++;\n                else if (ln.charAt(i) == \'O\')\n                    this.numO++;\n            }\n        }\n        \n        // X must either be one ahead or the same\n        return this.numX - this.numO == 0 || this.numX - this.numO == 1;\n    }\n    \n    public boolean checkForWin(String[] board, char c) {\n        // check horizontal\n        for (String ln : board) {\n            for (int i = 0; i < 3; i++) {\n                if (ln.charAt(0) == c && ln.charAt(1) == c && ln.charAt(2) == c) {\n                    return true;\n                }\n            }    \n        }\n        \n        // check vert\n        if ((board[0].charAt(0) == c && board[1].charAt(0) == c && board[2].charAt(0) == c) || (board[0].charAt(1) == c && board[1].charAt(1) == c && board[2].charAt(1) == c) || (board[0].charAt(2) == c && board[1].charAt(2) == c && board[2].charAt(2) == c))\n            return true;\n        \n        // check diagonal\n        if ((board[0].charAt(0) == c && board[1].charAt(1) == c && board[2].charAt(2) == c) || (board[0].charAt(2) == c && board[1].charAt(1)  == c && board[2].charAt(0) == c))\n            return true;\n\n        return false;\n    }\n}
['Java']