Description and comments on how to solve this - Java - easy solution
\n/*\n\nSo, basically the question is asking, we are given a tic tac toe board at some state\nwe should check and tell whether its a valid state or not\n\nThoug
```\n/*\n\nSo, basically the question is asking, we are given a tic tac toe board at some state\nwe should check and tell whether its a valid state or not\n\nThought process:\n1. We know X\'s always starts first, so count of X will be more by just 1 if X wins, otherwise invalid\n2. As X starts first, we cannot have count of O\'s more on the board, if thats the case, then its invalid\n3. When O wins the game, board must have equal no of O\'s and X\'s, otherwise the board is invalid\n4. The overall state of the board countX-countO should be 0 or 1.. Anything beyond is an invalid board\n*/\n\nclass Solution {\n    public boolean validTicTacToe(String[] board) {\n        int countX=0;\n        int countO=0;\n        \n        //count the no. of X and O on the board\n        for(String s: board){\n            \n            for(char c:s.toCharArray()){\n                \n                if(c==\'X\')\n                    countX++;\n                \n                if(c==\'O\')\n                    countO++;\n            }\n        }\n        \n        //if O\'s count is more than X - its invalid bcz X always starts first\n        //if X\'s count is more than O\'s by 1+, then its a invalid board\n        if(countO>countX || countX-countO>1)\n            return false;\n        \n        \n        //variables to see whether we find 3\'X or 3\'O - row wise, col wise or diagonal\n        boolean filledWithAllX=false;\n        boolean filledWithAllO=false;\n        \n        //checking row wise\n        for(String s:board){\n            \n            if(s.equals("XXX"))\n                filledWithAllX=true;\n            \n            if(s.equals("OOO"))\n                filledWithAllO=true;\n        }\n        \n        //-----------------------\n        //column wise check for X\'s\n        if(board[0].charAt(0)==\'X\' && board[1].charAt(0)==\'X\' && board[2].charAt(0)==\'X\')\n            filledWithAllX=true;\n        \n        if(board[0].charAt(1)==\'X\' && board[1].charAt(1)==\'X\' && board[2].charAt(1)==\'X\')\n            filledWithAllX=true;\n        \n        if(board[0].charAt(2)==\'X\' && board[1].charAt(2)==\'X\' && board[2].charAt(2)==\'X\')\n            filledWithAllX=true;\n       \n        //-----------------------\n        //column wise check for O\'s       \n        if(board[0].charAt(0)==\'O\' && board[1].charAt(0)==\'O\' && board[2].charAt(0)==\'O\')\n            filledWithAllO=true;\n        \n        if(board[0].charAt(1)==\'O\' && board[1].charAt(1)==\'O\' && board[2].charAt(1)==\'O\')\n            filledWithAllO=true;\n        \n        if(board[0].charAt(2)==\'O\' && board[1].charAt(2)==\'O\' && board[2].charAt(2)==\'O\')\n            filledWithAllO=true;\n        \n        //--------------------------     \n        //check for diagonals - we have 2 diagonals on 3x3 board\n        if(board[0].charAt(0)==\'O\' && board[1].charAt(1)==\'O\' && board[2].charAt(2)==\'O\')\n            filledWithAllO=true;\n        \n        if(board[0].charAt(0)==\'X\' && board[1].charAt(1)==\'X\' && board[2].charAt(2)==\'X\')\n            filledWithAllX=true;\n        \n        if(board[0].charAt(2)==\'O\' && board[1].charAt(1)==\'O\' && board[2].charAt(0)==\'O\')\n            filledWithAllO=true;\n        \n        if(board[0].charAt(2)==\'X\' && board[1].charAt(1)==\'X\' && board[2].charAt(0)==\'X\')\n            filledWithAllX=true;\n        \n        //if both are true, then its invalid board, bcz only one player can win\n        if(filledWithAllX && filledWithAllO)\n            return false;\n        \n        //when X wins, X\'s count should be more than O, as X starts first\n        if(filledWithAllX && (countX<=countO))\n            return false;\n        \n        //when O wins, O will be equal to X\'s count if not its invalid\n        if(filledWithAllO && (countO!=countX))\n            return false;\n        \n        return true;\n        \n    }\n}\n```
['Java']