C++ Intuitive Solution
The solution calculates the difference in number of X's and O's, and if either player has won. This is then used to check if: * The pieces are imbalanced (X pla
The solution calculates the difference in number of X's and O's, and if either player has won. This is then used to check if:
* The pieces are imbalanced (X plays first, so difference of X's - O's must be 1 or 0)
* Both players have won (not possible)
* X has won but O played another piece: the piece difference should be 0
* O has won but X played another piece: the piece difference should be 1

If none of the above conditions happen, the board is valid.

```
class Solution {
private:
    int boardCount(vector<string>& board, char piece) {
        int count = 0;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                count += (board[i][j] == piece);
            }
        }
        return count;
    }
    
    bool hasWin(vector<string>& board, char piece) {
        // row matches
        if (board[0][0] == piece && board[0][0] == board[0][1] && board[0][1] == board[0][2]) return true;
        if (board[1][0] == piece && board[1][0] == board[1][1] && board[1][1] == board[1][2]) return true;
        if (board[2][0] == piece && board[2][0] == board[2][1] && board[2][1] == board[2][2]) return true;
        // column matches
        if (board[0][0] == piece && board[0][0] == board[1][0] && board[1][0] == board[2][0]) return true;
        if (board[0][1] == piece && board[0][1] == board[1][1] && board[1][1] == board[2][1]) return true;
        if (board[0][2] == piece && board[0][2] == board[1][2] && board[1][2] == board[2][2]) return true;
        // diagonals
        if (board[0][0] == piece && board[0][0] == board[1][1] && board[1][1] == board[2][2]) return true;
        if (board[0][2] == piece && board[0][2] == board[1][1] && board[1][1] == board[2][0]) return true;
        return false;
    }
    
public:
    bool validTicTacToe(vector<string>& board) {
        int diff = boardCount(board, 'X') - boardCount(board, 'O');
        bool xWins = hasWin(board, 'X'), oWins = hasWin(board, 'O');
        if (diff != 1 && diff != 0)
            return false;
        if (xWins && oWins)
            return false;
        if (xWins && diff != 1)
            return false;
        if (oWins && diff != 0)
            return false;
        return true;
    }
};
```
[]