Solving Leetcode Interviews in Seconds with AI: Valid Tic-Tac-Toe State
Introduction
In this blog post, we will explore how to solve the LeetCode problem "794" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
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. 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 == 3 board[i].length == 3 board[i][j] is either 'X', 'O', or ' '.
Explanation
Here's the solution to the Tic-Tac-Toe validity problem:
- Count X's and O's: Determine the number of 'X's and 'O's on the board. In a valid game, the number of 'X's must be equal to the number of 'O's or one greater than the number of 'O's (because 'X' goes first). If this condition is violated, the board is invalid.
- Check for Winners: Check if 'X' or 'O' has won. It's invalid if both 'X' and 'O' have won, or if 'O' wins when the number of 'X's is greater than the number of 'O's, or if 'X' wins when the number of 'X's is equal to the number of 'O's (since 'X' goes first).
Validate Win Condition and Counts: The win condition must be consistent with the count of X's and O's.
Runtime & Storage Complexity: O(1) time complexity, O(1) space complexity. This is because the board size is fixed at 3x3.
Code
def validTicTacToe(board):
"""
Checks if a given Tic-Tac-Toe board is valid.
Args:
board (List[str]): A list of strings representing the Tic-Tac-Toe board.
Returns:
bool: True if the board is valid, False otherwise.
"""
x_count = 0
o_count = 0
for row in board:
for cell in row:
if cell == 'X':
x_count += 1
elif cell == 'O':
o_count += 1
if o_count > x_count or x_count - o_count > 1:
return False
def check_win(player):
for i in range(3):
if board[i][0] == player and board[i][1] == player and board[i][2] == player:
return True
if board[0][i] == player and board[1][i] == player and board[2][i] == player:
return True
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 False
x_wins = check_win('X')
o_wins = check_win('O')
if x_wins and o_wins:
return False
if x_wins and x_count == o_count:
return False
if o_wins and x_count > o_count:
return False
return True