Java faster than 100% Memory 99,66%
I was attempting to use some of the principles of "Clean Code" on this particular solution.  The purpose of this solution was to ensure proper naming of functio
I was attempting to use some of the principles of "Clean Code" on this particular solution.  The purpose of this solution was to ensure proper naming of functions/variables and to keep functions/methods short and limited to one principle. I also attempted to maintain readablility of the code from top to bottom.\n\nThe solution doesn\'t require the 2D gameBoard array, I just find that it makes processing a little more straightforward with minimal memory cost.\n\n\'\'\'\nclass Solution {\n    \n    private char[][] gameBoard = new char[3][3]; \n    \n    public boolean validTicTacToe(String[] board) {\n        \n        unpackBoard(board);\n\n        return gameBoardValidation();\n    }\n    \n    private void unpackBoard(String[] board){\n        for(int i=0; i<3;i++){\n            for(int j=0; j<3; j++){\n                this.gameBoard[i][j] = board[i].charAt(j);\n            }\n        }\n    }\n    \n    private boolean gameBoardValidation(){\n        \n        int numX, numO;\n        \n        numX = count(\'X\');\n        numO = count(\'O\');\n        \n        //X goes first\n        if(numO>numX) return false;\n        \n        //players take turns\n        if(numX>numO+1) return false;\n        \n        //both players can\'t win\n        if(winner(\'X\') && winner(\'O\')) return false;\n        \n        //game ends when one player wins\n        if(winner(\'X\') && numX==numO) return false;\n        \n        //game ends when one player wins\n        if(winner(\'O\') && numX>numO) return false;\n        \n        return true;\n    }\n    \n    private int count(char player){\n        \n        int num = 0;\n        \n        for(int i=0; i<3;i++){\n            for(int j=0; j<3; j++){\n                if(gameBoard[i][j] == player) num++;\n            }\n        }\n        \n        return num;\n    }\n    \n    private boolean winner(char player){\n        \n        if(validateRows(player)) return true;\n        if(validateColumns(player)) return true;\n        if(validateDiagonal(player)) return true;\n        \n        return false;\n    }\n    \n    private boolean validateRows(char player){\n        for(int i=0; i<3; i++){\n            if(gameBoard[i][0] == player && gameBoard[i][1] == player && gameBoard[i][2] == player) return true;\n        }\n        \n        return false;\n    }\n    \n    private boolean validateColumns(char player){\n        for(int i=0; i<3; i++){\n            if(gameBoard[0][i] == player && gameBoard[1][i] == player && gameBoard[2][i] == player) return true;\n        }\n        \n        return false;\n    }\n    \n    private boolean validateDiagonal(char player){\n        if(gameBoard[0][0] == player && gameBoard[1][1] == player && gameBoard[2][2] == player) return true;\n        if(gameBoard[0][2] == player && gameBoard[1][1] == player && gameBoard[2][0] == player) return true;\n        \n        return false;\n    }\n}\n\'\'\'
[]