[Java] Clean code - generic board solution
java\npublic boolean validTicTacToe(String[] board) {\n    int n = board.length;\n    int[] rows = new int[n];\n    int[] cols = new int[n];\n    int[] diagonal
```java\npublic boolean validTicTacToe(String[] board) {\n    int n = board.length;\n    int[] rows = new int[n];\n    int[] cols = new int[n];\n    int[] diagonals = new int[2];\n    \n    int turns = 0;\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            char ch = board[i].charAt(j);\n            if (ch == \'X\') {\n                turns++;\n                updateScores(rows, cols, diagonals, i, j, 1);\n            }\n            if (ch == \'O\') {\n                turns--;\n                updateScores(rows, cols, diagonals, i, j, -1);\n            }   \n        }\n    }\n    boolean isWonByX = isWon(rows, cols, diagonals, n);\n    boolean isWonByY = isWon(rows, cols, diagonals, -n);\n    \n    if (isWonByX && turns != 1 || isWonByY && turns != 0) return false;\n    \n    return turns == 0 || turns == 1;\n}\nprivate void updateScores(int[] rows, int[] cols, int[] diagonals, int r, int c, int score) {\n    rows[r] += score;\n    cols[c] += score;\n    \n    if (r == c) diagonals[0] += score;\n    if (r + c == rows.length - 1) diagonals[1] += score;\n}\n\nprivate boolean isWon(int[] rows, int[] cols, int[] diagonals, int scores) {\n    for (int i = 0; i < rows.length; i++) {\n        if (rows[i] == scores || cols[i] == scores)\n            return true;\n    }\n    return diagonals[0] == scores || diagonals[1] == scores;\n}\n```\n\n**Credit**  @mmao3
[]