Java short and crisp | beats 100% | with explanation
Algorithm:\n1. Turns should be played alternatively and first turn should be \'X\'.So at any time, it can be either \'X\' turn or \'O\' turn.\nIf no,of_X=no.of_
**Algorithm:**\n**1.** Turns should be played alternatively and first turn should be \'X\'.So at any time, it can be either \'X\' turn or \'O\' turn.\nIf no,of_X=no.of_O , then it is \'X\'s turn\nelse If no,of_X=1+no.of_O, then it is \'O\'s turn\nSo if other than this two conditions , return false.\n**2.** The game ends once, there are 3 of the same (non-empty) character filling any row, column, or diagonal.\nSo there cannot be more than 2 patterns of X, or more than two patterns O, or pattern of X and pattern of O together.\nThere is a special case when there can be two patterns at an instant , like\nX ,X ,**X**\n@,@,X\n@,@,X\nSo again by checking,\nIf pattern was \'X\',then next should be \'O, so check if no,of_X=1+no.of_O.\nIf pattern was \'O\',then next should be \'X, so check if no,of_X=no.of_O.\n```\nclass Solution {\n    public boolean validTicTacToe(String[] board) {\n        int x=0,o=0;\n        for(String s: board){\n            for(char ch : s.toCharArray()){\n                if(ch==\'X\') x++;\n                else if(ch==\'O\') o++;\n            }\n        }\n        if(!(x==o || x==o+1)) return false;\n        int x_pattern=checkPattern(board,\'X\');\n        int o_pattern=checkPattern(board,\'O\');\n        if(x_pattern>2 || o_pattern>2 || (x_pattern!=0 && o_pattern!=0)) return false;\n        if((x_pattern==2 || x_pattern==1) && x!=o+1) return false;\n        else if((o_pattern==2 || o_pattern==1 )&& x!=o) return false;\n        return true;\n    }\n    private int checkPattern(String[] board,Character ch){\n        int res=0;\n        //Rows\n        for(int i=0;i<3;i++){\n            int count=0;\n            for(int j=0;j<3;j++){\n                if(board[i].charAt(j)==ch) count++;\n            }\n            if(count==3) res++;\n        }\n        //Cols\n        for(int i=0;i<3;i++){\n            int count=0;\n            for(int j=0;j<3;j++){\n                if(board[j].charAt(i)==ch) count++;\n            }\n            if(count==3) res++;\n        }\n        //2 diagonals\n        if(board[0].charAt(0)==ch && board[1].charAt(1)==ch && board[2].charAt(2)==ch) res++;\n        if(board[0].charAt(2)==ch && board[1].charAt(1)==ch && board[2].charAt(0)==ch) res++;\n        return res;\n    }\n}\n```
[]