[Java] Simple Idea faster than 100%
The idea is:\n- Encode X = 1, O = -1, \' \' = 0\n- Calculate sums of row, column, diagnal, if any sum is 3, X wins; if any sum is -3, O wins\n- Valid if total s
The idea is:\n- Encode X = 1, O = -1, \' \' = 0\n- Calculate sums of row, column, diagnal, if any sum is 3, X wins; if any sum is -3, O wins\n- Valid if total sum of all is 0 (same Xs as Os), X cannot win; doesn\'t matter if O wins or not, still valid\n- Valid if total sum of all is 1 (one more Os than Xs), O cannot win; doesn\'t matter if X wins or not, still valid\n\n```\nclass Solution {\n    public boolean validTicTacToe(String[] board) {\n        //X=1,O=-1,\u2019 \u2019=0\n        int sum=0; \n        int[] wins_col=new int[3];\n        int[] wins_diag=new int[2];\n        Boolean x_win=Boolean.FALSE;Boolean o_win=Boolean.FALSE;\n        \n        for(int i=0;i<3;i++){\n            String str = board[i];\n            int row_sum=0;\n            for(int j=0;j<3;j++){\n                //char chr=str.toCharArray()[j];\n                int intchr=0;\n                if(str.toCharArray()[j]==\'X\')\n                    intchr=1;\n                if(str.toCharArray()[j]==\'O\')\n                    intchr=-1;\n                row_sum+=intchr;\n                wins_col[j]+=intchr;\n                if(i==2 && wins_col[j]==3)\n                    x_win=Boolean.TRUE;\n                if(i==2 && wins_col[j]==-3)\n                    o_win=Boolean.TRUE;\n\n                if(Math.abs(i-j)!=1){\n                    if(i==j && i == 1){\n                        wins_diag[0]+=intchr;\n                        wins_diag[1]+=intchr;\n                    }\n                    else if(i==j)\n                        wins_diag[0]+=intchr;\n                    else\n                        wins_diag[1]+=intchr;\n                }\n                if(i==2 && Math.max(wins_diag[0],wins_diag[1])==3)\n                    x_win=Boolean.TRUE;\n                if(i==2 && Math.min(wins_diag[0],wins_diag[1])==-3)\n                    o_win=Boolean.TRUE;\n            }\n            if(row_sum == 3)\n                x_win=Boolean.TRUE;\n            if(row_sum == -3)\n                o_win=Boolean.TRUE;\n\n            sum+=row_sum;\n\n        }\n        if(sum == 0 && !x_win)\n\t        return true;\n        if(sum == 1 && !o_win)\n            return true;\n\t    return false;\n    }\n}\n```\nRuntime: 0 ms, faster than 100.00% of Java online submissions for Valid Tic-Tac-Toe State.\nMemory Usage: 39.4 MB, less than 93.92% of Java online submissions for Valid Tic-Tac-Toe State.
[]