🔥BEATS 💯 % 🎯 |✨SUPER EASY BEGINNERS 👏| JAVA | C | C++ | PYTHON| JAVASCRIPT | DART 
IntuitionThe problem requires us to validate a Tic-Tac-Toe board state. The key observations are:
The number of 'X's must be equal to or one more than 'O's.
If
![image.png](https://assets.leetcode.com/users/images/33737f3e-d9fe-47b9-a773-b9cbf8f24ed6_1740135224.2450845.png)


---


# Intuition
The problem requires us to validate a Tic-Tac-Toe board state. The key observations are:
1. The number of 'X's must be equal to or one more than 'O's.
2. If a player has won, the board must reflect a valid move count (i.e., if 'X' wins, it should have one more move than 'O').
3. Both players cannot win simultaneously.

# Approach
1. **Count 'X' and 'O'**: Ensure 'X' is either equal to or one more than 'O'.
2. **Check for winners**: Identify if 'X' or 'O' has won using row, column, and diagonal checks.
3. **Validate the board state**:
   - If both 'X' and 'O' are winners, return `false`.
   - If 'X' wins, it must have one more move than 'O'.
   - If 'O' wins, both players must have the same move count.
4. **Return true if all conditions are satisfied**.

# Complexity
- **Time Complexity**: \(O(1)\) since we only perform a constant number of operations.
- **Space Complexity**: \(O(1)\) since we use only a few integer variables.


```dart []
class Solution {
  bool validTicTacToe(List<String> board) {
    int countX = 0, countO = 0;
    
    for (var row in board) {
      for (var cell in row.split('')) {
        if (cell == 'X') countX++;
        if (cell == 'O') countO++;
      }
    }

    if (countO > countX || countX > countO + 1) return false;

    bool xWins = isWinner(board, 'X');
    bool oWins = isWinner(board, 'O');

    if (xWins && oWins) return false;
    if (xWins && countX != countO + 1) return false;
    if (oWins && countX != countO) return false;

    return true;
  }

  bool isWinner(List<String> board, String player) {
    for (int i = 0; i < 3; i++) {
      if (board[i] == '$player$player$player' || 
          (board[0][i] == player &&
           board[1][i] == player &&
           board[2][i] == player)) {
        return true;
      }
    }
    return (board[0][0] == player &&
            board[1][1] == player &&
            board[2][2] == player) ||
           (board[0][2] == player &&
            board[1][1] == player &&
            board[2][0] == player);
  }
}
```


```python []
class Solution:
    def validTicTacToe(self, board: List[str]) -> bool:
        count_x = sum(row.count('X') for row in board)
        count_o = sum(row.count('O') for row in board)
        
        if count_o > count_x or count_x > count_o + 1:
            return False
        
        def is_winner(player: str) -> bool:
            return any(
                all(board[r][c] == player for c in range(3)) or
                all(board[r][c] == player for r in range(3))
                for r in range(3)
            ) or (
                board[0][0] == board[1][1] == board[2][2] == player or
                board[0][2] == board[1][1] == board[2][0] == player
            )
        
        x_wins, o_wins = is_winner('X'), is_winner('O')
        
        if x_wins and o_wins:
            return False
        if x_wins and count_x != count_o + 1:
            return False
        if o_wins and count_x != count_o:
            return False
        
        return True
```


```java []
class Solution {
    public boolean validTicTacToe(String[] board) {
        int countX = 0, countO = 0;
        
        for (String row : board) {
            for (char cell : row.toCharArray()) {
                if (cell == 'X') countX++;
                if (cell == 'O') countO++;
            }
        }
        
        if (countO > countX || countX > countO + 1) return false;
        
        boolean xWins = isWinner(board, 'X');
        boolean oWins = isWinner(board, 'O');

        if (xWins && oWins) return false;
        if (xWins && countX != countO + 1) return false;
        if (oWins && countX != countO) return false;
        
        return true;
    }
    
    private boolean isWinner(String[] board, char player) {
        for (int i = 0; i < 3; i++) {
            if (board[i].equals("" + player + player + player) || 
                (board[0].charAt(i) == player &&
                 board[1].charAt(i) == player &&
                 board[2].charAt(i) == player)) {
                return true;
            }
        }
        return (board[0].charAt(0) == player &&
                board[1].charAt(1) == player &&
                board[2].charAt(2) == player) ||
               (board[0].charAt(2) == player &&
                board[1].charAt(1) == player &&
                board[2].charAt(0) == player);
    }
}
```


```cpp []
class Solution {
public:
    bool validTicTacToe(vector<string>& board) {
        int countX = 0, countO = 0;
        for (string row : board) {
            for (char cell : row) {
                if (cell == 'X') countX++;
                if (cell == 'O') countO++;
            }
        }
        
        if (countO > countX || countX > countO + 1) return false;
        
        bool xWins = isWinner(board, 'X');
        bool oWins = isWinner(board, 'O');

        if (xWins && oWins) return false;
        if (xWins && countX != countO + 1) return false;
        if (oWins && countX != countO) return false;
        
        return true;
    }
    
    bool isWinner(vector<string>& board, char player) {
        for (int i = 0; i < 3; i++) {
            if (board[i] == string(3, player) || 
                (board[0][i] == player &&
                 board[1][i] == player &&
                 board[2][i] == player)) {
                return true;
            }
        }
        return (board[0][0] == player &&
                board[1][1] == player &&
                board[2][2] == player) ||
               (board[0][2] == player &&
                board[1][1] == player &&
                board[2][0] == player);
    }
};
```


```go []
func validTicTacToe(board []string) bool {
    countX, countO := 0, 0
    for _, row := range board {
        for _, cell := range row {
            if cell == 'X' {
                countX++
            } else if cell == 'O' {
                countO++
            }
        }
    }

    if countO > countX || countX > countO+1 {
        return false
    }

    xWins := isWinner(board, 'X')
    oWins := isWinner(board, 'O')

    if xWins && oWins {
        return false
    }
    if xWins && countX != countO+1 {
        return false
    }
    if oWins && countX != countO {
        return false
    }

    return true
}

func isWinner(board []string, player rune) bool {
    for i := 0; i < 3; i++ {
        if board[i] == string([]rune{player, player, player}) || 
           (board[0][i] == player && board[1][i] == player && board[2][i] == player) {
            return true
        }
    }
    return (board[0][0] == player && board[1][1] == player && board[2][2] == player) ||
           (board[0][2] == player && board[1][1] == player && board[2][0] == player)
}
```



-----

![image.png](https://assets.leetcode.com/users/images/d81a7cee-fa73-41a2-82dc-65bf475d960c_1722267693.3902693.png) {:style='width:250px'}

['Array', 'C', 'Matrix', 'Python', 'C++', 'Java', 'Python3', 'JavaScript', 'Dart', 'Python ML']