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:
' '.'X' characters, while the second player always places 'O' characters.'X' and 'O' characters are always placed into empty squares, never filled ones.Example 1:
Input: board = ["O "," "," "] Output: false Explanation: The first player always plays "X".
Example 2:
Input: board = ["XOX"," X "," "] Output: false Explanation: Players take turns making moves.
Example 3:
Input: board = ["XOX","O O","XOX"] Output: true
Constraints:
board.length == 3board[i].length == 3board[i][j] is either 'X', 'O', or ' '.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to validating a Tic-Tac-Toe board means we check every possible condition that makes a board valid or invalid. We will exhaustively look at all the possibilities for wins and number of moves, marking the board as valid only if it satisfies all criteria.
Here's how the algorithm would work step-by-step:
def valid_tic_tac_toe_state(board):
number_of_x = 0
number_of_o = 0
for row in board:
for cell in row:
if cell == 'X':
number_of_x += 1
elif cell == 'O':
number_of_o += 1
if number_of_o > number_of_x or number_of_x - number_of_o > 1:
return False
x_wins = check_win(board, 'X')
o_wins = check_win(board, 'O')
# If both win, the game state is invalid
if x_wins and o_wins:
return False
if x_wins:
# If 'X' wins, there cannot be more 'O's than 'X's
if number_of_o > number_of_x:
return False
if o_wins:
# If 'O' wins, 'X' and 'O' must have the same number of pieces
if number_of_x != number_of_o:
return False
#If no one won, consider the count of marks
if not x_wins and not o_wins:
if number_of_x == number_of_o or number_of_x == number_of_o + 1:
return True
else:
return False
return True
def check_win(board, player):
# Check rows
for row in board:
if all(cell == player for cell in row):
return True
# Check columns
for column in range(3):
if all(board[row][column] == player for row in range(3)):
return True
# Check diagonals
if (board[0][0] == player and board[1][1] == player and board[2][2] == player):
return True
if (board[0][2] == player and board[1][1] == player and board[2][0] == player):
return True
return FalseTo determine if a Tic-Tac-Toe board is valid, we need to check if the number of 'X's and 'O's are within the allowed range and if there is a valid winner. The key is to efficiently count the marks and check for winning conditions without overcomplicating the logic.
Here's how the algorithm would work step-by-step:
def valid_tic_tac_toe_state(board):
number_of_x = 0
number_of_o = 0
for row in board:
for cell in row:
if cell == 'X':
number_of_x += 1
elif cell == 'O':
number_of_o += 1
# Check if the difference between Xs and Os is valid.
if not (number_of_x == number_of_o or number_of_x == number_of_o + 1):
return False
def check_win(mark):
# Check rows
for row in board:
if all(cell == mark for cell in row):
return True
# Check columns
for column in range(3):
if all(board[row][column] == mark for row in range(3)):
return True
# Check diagonals
if (board[0][0] == mark and board[1][1] == mark and board[2][2] == mark):
return True
if (board[0][2] == mark and board[1][1] == mark and board[2][0] == mark):
return True
return False
x_wins = check_win('X')
o_wins = check_win('O')
# If both X and O win, the board is invalid.
if x_wins and o_wins:
return False
# Validate winner based on X and O counts.
if x_wins:
if number_of_x != number_of_o + 1:
return False
if o_wins:
if number_of_x != number_of_o:
return False
return True| Case | How to Handle |
|---|---|
| Null or empty board | Return true (or false, depending on specified expected behavior for invalid input) immediately since it's not a valid game state. |
| Board with invalid characters (not 'X', 'O', or ' ') | Return false, as the board contains illegal characters, violating Tic-Tac-Toe rules. |
| More 'O's than 'X's | Return false, since 'X' always plays first or the same number as 'O'. |
| Too many 'X's or 'O's relative to board size | Return false, as it would be impossible to make that many moves in a valid Tic-Tac-Toe game on the given board. |
| Both 'X' and 'O' have won | Return false, because in a standard Tic-Tac-Toe game, only one player can win. |
| 'X' won, but it's 'O's turn | Return false, as the game should have ended when 'X' won. |
| 'O' won, but it's 'X's turn and the number of 'X's and 'O's are equal | Return false because 'X' made the last move and cannot be behind. |
| Board is full, but no one has won | Return true, which is a valid tie game state. |