Python | Simple state representation for any TicTacToe | Memory 99.38%
Everyone has solved this question pretty smoothly and efficiently.\nI just wanted to add a way of representing the state in a different way.\n\nI am using an ar
Everyone has solved this question pretty smoothly and efficiently.\nI just wanted to add a way of representing the state in a different way.\n\nI am using an **array of size 8** for the representation.\nThe **first 3 indexes** represent the state of the **rows**\nThe **next 3** represent the **columns**\nThe **last 2** represent the **diagonals**\n\nAfter that we just check for win condition in the array and design our cases in that way\nI have tried to explain it in the code itself.\nIf you have any doubts, please feel free to ask in the comments below\n\n```\nclass Solution:\n    def validTicTacToe(self, board: List[str]) -> bool:\n        # count the number of x and o\n        xCount = oCount = 0\n        \n        # Easy state representation of valid win lines\n        # Indexes: 0,1,2 represent rows. 3,4,5 represent columns. 6,7 represent diagonals (6 - \\, 7 - /)\n        arr = [0]*8\n\n        # Check over the whole board\n        for i, row in enumerate(board):\n            for j, ch in enumerate(row):\n\n                # Check for X\n                # steps: \n                # 1. increment xCount \n                # 2. add 1 to the row\n                # 3. add 1 to the column\n                # 4. add 1 to the diagonals if coordinates lie on diagonals\n                if ch == \'X\': \n                    xCount += 1\n                    \n                    arr[i] += 1\n                    arr[j+3] += 1\n                    \n                    # check left to right diagonal (\\)\n                    if i == j: arr[6] += 1\n                    # check right to left diagonal (/)\n                    if i == 2-j: arr[7] += 1\n\n                # Check for O\n                # steps: \n                # 1. increment oCount \n                # 2. subtract 1 from the row\n                # 3. subtract 1 from the column\n                # 4. subtract 1 from the diagonals if coordinates lie on diagonals\n                if ch == \'O\': \n                    oCount += 1\n                    \n                    arr[i] -= 1\n                    arr[j+3] -= 1\n                    \n                    # check left to right diagonal (\\)\n                    if i == j: arr[6] -= 1\n                    # check right to left diagonal (/)\n                    if i == 2-j: arr[7] -= 1\n                        \n        # win conditions:\n        # if 3 exists in array: X wins\n        # if -3 exists: O wins\n        # For more visualization, try drawing this\n        xWin, oWin = 3 in arr, -3 in arr\n        \n        # Case 1: both are shown as winning (invalid state as X wins before O) eg: ["XXX","   ","OOO"]\n        if xWin and oWin: return False\n\n        # Case 2: X wins that indicates that the difference in count should be 1. As X plays before O\n        if xWin and xCount - oCount != 1 : return False\n        \n        # Case 3: O wins that indicates that the difference in count should be 0. As X plays before O\n        if oWin and xCount - oCount != 0 : return False\n        \n        # Case 4: both of them have to play alternate moves, hence difference can only be 1 or 0\n        if xCount - oCount not in (0,1): return False\n        \n        # If all cases pass, board is valid\n        return True\n```\n\nOutput: \n![image](https://assets.leetcode.com/users/images/a5c5553d-a65e-4918-b609-9e33de8950ac_1656934391.704926.png)\n
['Python']