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.
Here are the 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.
Examples
Example 1:
$$ \begin{array}{|c|c|c|} \hline \colorbox{orange} O & & \\ \hline & & \\ \hline & & \\ \hline \end{array} $$
|
|
Example 2:
$$ \begin{array}{|c|c|c|} \hline \colorbox{lightblue} X & \colorbox{orange} O & \colorbox{lightblue} X \\ \hline & \colorbox{lightblue} X & \\ \hline & & \\ \hline \end{array} $$
|
|
Example 3:
$$ \begin{array}{|c|c|c|} \hline \colorbox{lightblue} X & \colorbox{orange} O & \colorbox{lightblue} X \\ \hline \colorbox{orange} O & & \colorbox{orange} O \\ \hline \colorbox{lightblue} X & \colorbox{orange} O & \colorbox{lightblue} X \\ \hline \end{array} $$
|
|
Solution
Method 1 - Simple Heuristics
Here are the key points:
- Player Turn Rules:
- Since player ‘X’ always starts, the count of ‘X’ should be greater than or equal to the count of ‘O’. Therefore, if
o_count > x_count
, the board is invalid. - Players alternate turns, so the difference between the count of ‘X’ and ‘O’ should not exceed one. Thus, if
x_count - o_count > 1
, the board is invalid.
- Since player ‘X’ always starts, the count of ‘X’ should be greater than or equal to the count of ‘O’. Therefore, if
- Winning Conditions:
- If player ‘O’ has a winning condition, ensure that player ‘X’ does not have a winning condition at the same time. This is because the game should stop as soon as a winning condition is met.
- If player ‘O’ wins, then
x_count
should equalo_count
since ‘O’ plays second and the counts must match up to this condition. - If player ‘X’ wins, then
x_count
should be exactly one more thano_count
because ‘X’ starts first.
- Algorithm:
- Check initial conditions for count validity.
- Evaluate the board to see if either player has a winning configuration.
- Validate that the counts and winning conditions align correctly based on the rules of the game.
Here is a more detailed explanation:
- Check Initial Count Validity:
- If
o_count > x_count
, returnFalse
. - If
x_count - o_count > 1
, returnFalse
.
- If
- Check Winning Conditions:
- If player ‘O’ wins:
- Ensure player ‘X’ does not also win. If both win, return
False
. - Ensure
x_count == o_count
. If not, returnFalse
.
- Ensure player ‘X’ does not also win. If both win, return
- If player ‘X’ wins:
- Ensure
x_count == o_count + 1
. If not, returnFalse
.
- Ensure
- If player ‘O’ wins:
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(1)
, because the board size is fixed at3 x 3
. Checking each cell and verifying win conditions is constant. - 🧺 Space complexity:
O(1)
, as no additional space proportional to input size is required beyond a few counters and boolean flags.