<p>Given a Tic-Tac-Toe board as a string array <code>board</code>, return <code>true</code> if and only if it is possible to reach this board position during the course of a valid tic-tac-toe game.</p>

<p>The board is a <code>3 x 3</code> array that consists of characters <code>&#39; &#39;</code>, <code>&#39;X&#39;</code>, and <code>&#39;O&#39;</code>. The <code>&#39; &#39;</code> character represents an empty square.</p>

<p>Here are the rules of Tic-Tac-Toe:</p>

<ul>
	<li>Players take turns placing characters into empty squares <code>&#39; &#39;</code>.</li>
	<li>The first player always places <code>&#39;X&#39;</code> characters, while the second player always places <code>&#39;O&#39;</code> characters.</li>
	<li><code>&#39;X&#39;</code> and <code>&#39;O&#39;</code> characters are always placed into empty squares, never filled ones.</li>
	<li>The game ends when there are three of the same (non-empty) character filling any row, column, or diagonal.</li>
	<li>The game also ends if all squares are non-empty.</li>
	<li>No more moves can be played if the game is over.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/15/tictactoe1-grid.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> board = [&quot;O  &quot;,&quot;   &quot;,&quot;   &quot;]
<strong>Output:</strong> false
<strong>Explanation:</strong> The first player always plays &quot;X&quot;.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/15/tictactoe2-grid.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> board = [&quot;XOX&quot;,&quot; X &quot;,&quot;   &quot;]
<strong>Output:</strong> false
<strong>Explanation:</strong> Players take turns making moves.
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/15/tictactoe4-grid.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> board = [&quot;XOX&quot;,&quot;O O&quot;,&quot;XOX&quot;]
<strong>Output:</strong> true
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>board.length == 3</code></li>
	<li><code>board[i].length == 3</code></li>
	<li><code>board[i][j]</code> is either <code>&#39;X&#39;</code>, <code>&#39;O&#39;</code>, or <code>&#39; &#39;</code>.</li>
</ul>

class Solution:
    def validTicTacToe(self, board: List[str]) -> bool:
        def isWin(ch):
            nonlocal n 
            rows = [0] * n           # rows[i]: sum of values in row i
            cols = [0] * n           # cols[j]: sum of values in clo j
            d1 = 0                   # diagonal sum
            d2 = 0                   # anti diagonal sum

            # fill out rows
            for i in range(n):
                summ = 0 
                for j in range(n):
                    if board[i][j] == ch:
                        summ += 1 
                rows[i] = summ

            # fill out cols
            for j in range(n):
                summ = 0 
                for i in range(n):
                    if board[i][j] == ch:
                        summ += 1 
                cols[j] = summ

            # diagonal and anti diagonal cases
            for i in range(n):
                for j in range(n):
                    if i == j and board[i][j] == ch:
                        d1 += 1

                    if i + j + 1 == n and board[i][j] == ch:
                        d2 += 1

            return any(count == n for count in rows) or \
                    any(count == n for count in cols) or \
                    d1 == n or d2 == n

        n = len(board)

        count_X, count_O = 0, 0

        for i in range(n):
            for j in range(n):
                if board[i][j] == 'X':
                    count_X += 1
                if board[i][j] == 'O':
                    count_O += 1

        # X is always greater and the differnce at max is 1
        if count_X < count_O or count_X - count_O > 1:
            return False

        # if X is the winner, so count_X - count_O = 1, otherwise it is not valid
        # if O is the winner, so count_X - count_O = 0, otherwise it is not valid
        if isWin('X') and count_X == count_O or isWin('O') and count_X != count_O:
            return False

        return True
23