@0ms || Java with comments
\tclass Solution {\n\n\t//     This function will count the number of character c in board\n\t\tpublic int count(char[][] board, char c) {\n\t\t\tint count = 0;
\tclass Solution {\n\n\t//     This function will count the number of character c in board\n\t\tpublic int count(char[][] board, char c) {\n\t\t\tint count = 0;\n\t\t\tfor(int i=0; i<board.length; i++) {\n\t\t\t\tfor(char ce: board[i]) {\n\t\t\t\t\tif(ce == c) count++;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn count;\n\t\t}\n\n\t//     Valid state is when 2 rows or 2 columns doesn\'t have the winning state.\n\t\tpublic char checkValidState(char[][] board) {\n\t\t\tchar winner = \'!\';\n\t\t\tint count = 0;\n\t\t\tfor(int i=0; i<board.length; i++) {\n\t\t\t\tif(board[i][0]!= \' \' && board[i][0] == board[i][1] && board[i][1] == board[i][2]) {\n\t\t\t\t\twinner = board[i][0];\n\t\t\t\t\tcount++;\n\t\t\t\t}\n\t\t\t}\n\t//         Two rows have winning state hence wrong \n\t\t\tif(count > 1) return \'?\';\n\t\t\tcount = 0;\n\t\t\tfor(int j=0; j<board[0].length; j++) {\n\t\t\t\tif(board[0][j]!= \' \' && board[0][j] == board[1][j] && board[1][j] == board[2][j]) {\n\t\t\t\t\twinner = board[0][j];\n\t\t\t\t\tcount++;\n\t\t\t\t}\n\t\t\t}\n\t//         Two columns have winning state hence wrong \n\t\t\tif(count > 1) return \'?\';\n\t//         Winning states an also be present in 1 of the 2 diagnals\n\t\t\tif(board[0][0] != \' \' && board[0][0] == board[1][1] && board[1][1] == board[2][2]) winner = board[0][0];\n\t\t\tif(board[0][2] != \' \' && board[0][2] == board[1][1] && board[1][1] == board[2][0]) winner = board[2][0];\n\t\t\treturn winner;\n\t\t}\n\n\n\n\t\tpublic boolean validTicTacToe(String[] b) {\n\n\t\t\tchar[][] board = new char[3][3];\n\t\t\tfor(int i=0; i<b.length; i++) {\n\t\t\t\tfor(int j=0; j<b[i].length(); j++) {\n\t\t\t\t\tboard[i][j] = b[i].charAt(j);\n\t\t\t\t}\n\t\t\t}\n\t\t\tint X = count(board, \'X\');\n\t\t\tint O = count(board, \'O\');\n\n\t//         X-O == 1 || X-O == 0 Eg: 1-> X=1, O=0, 2-> X=2, O=1, 3->X=2, O=1 .....\n\t\t\tif(X-O != 1 && X-O != 0) return false;\n\t//         \'?\' will be stored in result if state is not valid \'!\' will be stored if state is valid and\n\t//         no one has win the game. If X wins the game \'X\' will be stored similarly \'O\'\n\t\t\tchar result = checkValidState(board);\n\t//         \'?\' stored implies not valid state\n\t\t\tif(result == \'?\') return false;\n\t//         \'!\' stored implies state valid but no one has win the game.\n\t\t\tif(result == \'!\') return true;\n\t//         X can only win the game if number of X is greater than number of O\n\t\t\tif(X>O) {\n\t\t\t\tif(result == \'X\') return true;\n\t\t\t\treturn false;\n\t\t\t}\n\t//         O can only win the game if number of O is equal to number of X\n\t\t\tif(result == \'O\') return true;\n\t\t\treturn false;\n\t\t}\n\t}
[]