[Python] For a n*n board, with explanation
In real interviews, it is possible the interviewer will ask what if it is an n*n board, and what if we want to adjust our rules, etc. \nSo it will be helpful to
In real interviews, it is possible the interviewer will ask what if it is an `n*n` board, and what if we want to adjust our rules, etc. \nSo it will be helpful to use a helper function, and even if we adjust our rules, we just need to refactor our helper function, as the general valid-or-not logic remains the same. \nHere is an implemenation for a `n*n` board:\n\n```\nclass Solution:\n    def validTicTacToe(self, board: List[str]) -> bool:\n        # case 1 &2 : X.amound < O.amount or X.amount - O.amount >1\n        if sum(row.count(\'X\') for row in board) < sum(row.count(\'O\') for row in board) \\\n        or sum(row.count(\'X\') for row in board) - sum(row.count(\'O\') for row in board)>1:\n            return False\n        # case 3: somebody already has a winning state\n        if self.iswin(board, \'X\'):\n            if sum(row.count(\'X\') for row in board) - sum(row.count(\'O\') for row in board)!=1:\n                return False\n        if self.iswin(board, \'O\'):\n            if sum(row.count(\'X\') for row in board) != sum(row.count(\'O\') for row in board):\n                return False\n        return True\n    \n    def iswin(self, board, target):\n        # n*n board\n        if any(row == target*len(board) for row in board):\n            return True\n        for c in range(len(board[0])):\n            if all(board[r][c] == target for r in range(len(board))):\n                return True\n        # diagonal\n        diag = [(i,i) for i in range(len(board))]\n        reverse_diag = [(i, len(board)-1-i) for i in range(len(board))]\n        if all(board[x][y] == target for x,y in diag) or all(board[x][y] == target for x,y in reverse_diag):\n            return True\n        return False\n```
[]