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
Valid Tic-Tac-Toe State: Game Rule ValidationInput BoardX O XO XX O X3x3 game boardValidation Rules1. X goes first (turn order)2. Only one player can win3. Game stops after winGame rules to checkResultINVALIDX=5, O=3Too many X movesfalseProcess: Count pieces → Check turn order → Validate win conditions → Return resultThis board violates turn order: X has made 5 moves but O only 3
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
Asked in
Google 15 Amazon 12 Microsoft 10 Apple 8
28.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen