VOOZH about

URL: https://www.geeksforgeeks.org/dsa/validity-of-a-given-tic-tac-toe-board-configuration/

⇱ Validity of a given Tic-Tac-Toe board configuration - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Validity of a given Tic-Tac-Toe board configuration

Last Updated : 23 Jul, 2025

A Tic-Tac-Toe board is given after some moves are played. Find out if the given board is valid, i.e., is it possible to reach this board position after some moves or not.
Note that every arbitrary filled grid of 9 spaces isn't valid e.g. a grid filled with 3 X and 6 O isn't valid situation because each player needs to take alternate turns.

👁 tictactoe

Input is given as a 1D array of size 9. 

Examples:

Input: board[] = {'X', 'X', 'O', 
'O', 'O', 'X',
'X', 'O', 'X'};
Output: Valid
Input: board[] = {'O', 'X', 'X',
'O', 'X', 'X',
'O', 'O', 'X'};
Output: Invalid
(Both X and O cannot win)
Input: board[] = {'O', 'X', ' ',
' ', ' ', ' ',
' ', ' ', ' '};
Output: Valid
(Valid board with only two moves played)
Recommended Practice

Basically, to find the validity of an input grid, we can think of the conditions when an input grid is invalid. Let no. of "X"s be countX and no. of "O"s be countO. Since we know that the game starts with X, a given grid of Tic-Tac-Toe game would be definitely invalid if following two conditions meet 

  1. countX != countO AND 
  2. countX != countO + 1
    • Since "X" is always the first move, second condition is also required.
    • Now does it mean that all the remaining board positions are valid one? The answer is NO. Think of the cases when input grid is such that both X and O are making straight lines. This is also not
    • valid position because the game ends when one player wins. So we need to check the following condition as well 
  3. If input grid shows that both the players are in winning situation, it's an invalid position. 
  4. If input grid shows that the player with O has put a straight-line (i.e. is in win condition) and countX != countO, it's an invalid position. The reason is that O plays his move only after X plays his
    • move. Since X has started the game, O would win when both X and O has played equal no. of moves. 
  5. If input grid shows that X is in winning condition than xCount must be one greater that oCount.
    • Armed with above conditions i.e. a), b), c) and d), we can now easily formulate an algorithm/program to check the validity of a given Tic-Tac-Toe board position. 
1) countX == countO or countX == countO + 1
2) If O is in win condition then check
a) If X also wins, not valid
b) If xbox != obox , not valid
3) If X is in win condition then check if xCount is
one more than oCount or not

Another way to find the validity of a given board is using 'inverse method' i.e. rule out all the possibilities when a given board is invalid.


Output
Given board is valid

Time complexity: O(1)
Auxiliary Space: O(1), since no extra space has been taken.

Approach 2:

The algorithm to check if a Tic-Tac-Toe board is valid or not is as follows:

  • Initialize a 2D array win of size 8x3, which contains all possible winning combinations in Tic-Tac-Toe. Each row of the win array represents a winning combination, and each element in a row represents a cell index on the board.
  • Define a function isCWin(board, c) which takes a board configuration board and a character c ('X' or 'O') as inputs, and returns true if character c has won on the board.
  • Inside the isCWin function, iterate over each row of the win array. Check if the board has the same character c at all three cell indices of the current row. If yes, return true, as the character c has won.
  • Define a function isValid(board) which takes a board configuration board as input, and returns true if the board is valid, else returns false.
  • Inside the isValid function, count the number of 'X' and 'O' characters on the board, and store them in xCount and oCount variables, respectively.
  • The board can be valid only if either xCount and oCount are the same, or xCount is one more than oCount.
  • If 'O' is a winner on the board, check if 'X' is also a winner. If yes, return false as both 'X' and 'O' cannot win at the same time. If not, return true if xCount and oCount are the same, else return false.
  • If 'X' is a winner on the board, then xCount must be one more than oCount. If not, return false.
  • If 'O' is not a winner, return true as the board is valid.

Here is the code of the above approach:


Output
Given board is valid


Time complexity: O(N^2)
Auxiliary Space: O(N)

Thanks to Utkarsh for suggesting this solution. This article is contributed by Aarti_Rathi and Utkarsh.

Comment
Article Tags: