Valid Tic-Tac-Toe State - Problem
Given a Tic-Tac-Toe board as a string array board, return true if and only if it is possible to reach this board position during the course of a valid tic-tac-toe game.
The board is a 3 x 3 array that consists of characters ' ', 'X', and 'O'. The ' ' character represents an empty square.
Rules of Tic-Tac-Toe:
- Players take turns placing characters into empty squares
' ' - The first player always places
'X'characters, while the second player always places'O'characters 'X'and'O'characters are always placed into empty squares, never filled ones- The game ends when there are three of the same (non-empty) character filling any row, column, or diagonal
- The game also ends if all squares are non-empty
- No more moves can be played if the game is over
Input & Output
Example 1 — Valid Game State
$
Input:
board = ["O ", " ", " "]
›
Output:
false
💡 Note:
O went first, but X should always go first. This violates the turn order rule.
Example 2 — Valid Game State
$
Input:
board = ["XOX", " X ", " "]
›
Output:
false
💡 Note:
X has 3 moves and O has 1 move. Since X goes first, this means O should have made 2 moves. Invalid turn count.
Example 3 — Valid End State
$
Input:
board = ["XXX", " ", "OOO"]
›
Output:
false
💡 Note:
Both X and O have winning lines, which is impossible in a valid game. Only one player can win.
Constraints
- board.length == 3
- board[i].length == 3
- board[i][j] is either 'X', 'O', or ' '
Visualization
Tap to expand
Understanding the Visualization
1
Input Board
3x3 tic-tac-toe board with X, O, and empty spaces
2
Rule Validation
Check turn order, win conditions, and game state rules
3
Valid/Invalid
Return true if board state is reachable, false otherwise
Key Takeaway
🎯 Key Insight: Valid tic-tac-toe states must follow turn order (X first) and game-ending rules
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code