810. Valid Tic-Tac-Toe State
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to validate a Tic-Tac-Toe board state. A valid state is one that could be reached during a normal game. This requires checking the board against the rules of Tic-Tac-Toe, specifically focusing on turn order and winning conditions.
Solution
Valid Tic-Tac-Toe State in Python
class Solution:
def validTicTacToe(self, board: List[str]) -> bool:
x_count = sum(row.count('X') for row in board)
o_count = sum(row.count('O') for row in board)
# Rule 1: Turn balance. 'X' moves first.
# The number of 'O's cannot be more than 'X's.
# The number of 'X's cannot be more than 'O's by more than 1.
if not (x_count == o_count or x_count == o_count + 1):
return False
def check_win(player: str) -> bool:
# Check rows
for i in range(3):
if all(board[i][j] == player for j in range(3)):
return True
# Check columns
for j in range(3):
if all(board[i][j] == player for i in range(3)):
return True
# Check diagonals
if all(board[i][i] == player for i in range(3)):
return True
if all(board[i][2 - i] == player for i in range(3)):
return True
return False
x_wins = check_win('X')
o_wins = check_win('O')
# Rule 2: If X wins, the number of X's must be one more than O's.
# If x_count == o_count, it implies O just made a move, which is impossible if X has already won.
if x_wins and x_count == o_count:
return False
# Rule 3: If O wins, the number of X's must be equal to O's.
# If x_count > o_count, it implies X just made a move, which is impossible if O has already won.
if o_wins and x_count > o_count:
return False
# Note: The case where both win is implicitly handled.
return True
Valid Tic-Tac-Toe State in Java
class Solution {
public boolean validTicTacToe(String[] board) {
int xCount = 0;
int oCount = 0;
// Count the number of 'X's and 'O's.
for (String row : board) {
for (char c : row.toCharArray()) {
if (c == 'X') {
xCount++;
} else if (c == 'O') {
oCount++;
}
}
}
// Rule 1: Turn balance. 'X' moves first.
// The number of 'O's cannot be more than 'X's.
// The number of 'X's cannot be more than 'O's by more than 1.
if (oCount > xCount || xCount > oCount + 1) {
return false;
}
boolean xWins = checkWin('X', board);
boolean oWins = checkWin('O', board);
// Rule 2: If X wins, the number of X's must be one more than O's.
// If xCount == oCount, it implies O just made a move, which is impossible if X has already won.
if (xWins && xCount == oCount) {
return false;
}
// Rule 3: If O wins, the number of X's must be equal to O's.
// If xCount > oCount, it implies X just made a move, which is impossible if O has already won.
if (oWins && xCount > oCount) {
return false;
}
// Note: The case where both win is implicitly handled.
// If both xWins and oWins are true, one of the above conditions must be met.
return true;
}
// Helper function to check if a player has won.
private boolean checkWin(char player, String[] board) {
// Check rows
for (int i = 0; i < 3; i++) {
if (board[i].charAt(0) == player && board[i].charAt(1) == player && board[i].charAt(2) == player) {
return true;
}
}
// Check columns
for (int i = 0; i < 3; i++) {
if (board[0].charAt(i) == player && board[1].charAt(i) == player && board[2].charAt(i) == player) {
return true;
}
}
// Check diagonals
if (board[0].charAt(0) == player && board[1].charAt(1) == player && board[2].charAt(2) == player) {
return true;
}
if (board[0].charAt(2) == player && board[1].charAt(1) == player && board[2].charAt(0) == player) {
return true;
}
return false;
}
}
Valid Tic-Tac-Toe State in C++
class Solution {
public:
bool validTicTacToe(vector<string>& board) {
int x_count = 0;
int o_count = 0;
// Count the number of 'X's and 'O's.
for (const string& row : board) {
for (char c : row) {
if (c == 'X') {
x_count++;
} else if (c == 'O') {
o_count++;
}
}
}
// Rule 1: Turn balance. 'X' moves first.
// The number of 'O's cannot be more than 'X's.
// The number of 'X's cannot be more than 'O's by more than 1.
if (o_count > x_count || x_count > o_count + 1) {
return false;
}
bool x_wins = check_win('X', board);
bool o_wins = check_win('O', board);
// Rule 2: If X wins, the number of X's must be one more than O's.
// If x_count == o_count, it implies O just made a move, which is impossible if X has already won.
if (x_wins && x_count == o_count) {
return false;
}
// Rule 3: If O wins, the number of X's must be equal to O's.
// If x_count > o_count, it implies X just made a move, which is impossible if O has already won.
if (o_wins && x_count > o_count) {
return false;
}
// Note: The case where both win is implicitly handled.
// If both x_wins and o_wins are true, one of the above conditions must be met.
// e.g., if x_count == o_count, the x_wins check fails it.
// e.g., if x_count == o_count + 1, the o_wins check fails it.
return true;
}
private:
// Helper function to check if a player has won.
bool check_win(char player, const vector<string>& board) {
// Check rows
for (int i = 0; i < 3; ++i) {
if (board[i][0] == player && board[i][1] == player && board[i][2] == player) {
return true;
}
}
// Check columns
for (int i = 0; i < 3; ++i) {
if (board[0][i] == player && board[1][i] == player && board[2][i] == player) {
return true;
}
}
// Check diagonals
if (board[0][0] == player && board[1][1] == player && board[2][2] == player) {
return true;
}
if (board[0][2] == player && board[1][1] == player && board[2][0] == player) {
return true;
}
return false;
}
};
Valid Tic-Tac-Toe State in TypeScript
function validTicTacToe(board: string[]): boolean {
let xCount = 0;
let oCount = 0;
// Count the number of 'X's and 'O's.
for (const row of board) {
for (const char of row) {
if (char === 'X') {
xCount++;
} else if (char === 'O') {
oCount++;
}
}
}
// Rule 1: Turn balance. 'X' moves first.
// The number of 'O's cannot be more than 'X's.
// The number of 'X's cannot be more than 'O's by more than 1.
if (oCount > xCount || xCount > oCount + 1) {
return false;
}
const checkWin = (player: 'X' | 'O'): boolean => {
// Check rows
for (let i = 0; i < 3; i++) {
if (board[i][0] === player && board[i][1] === player && board[i][2] === player) {
return true;
}
}
// Check columns
for (let j = 0; j < 3; j++) {
if (board[0][j] === player && board[1][j] === player && board[2][j] === player) {
return true;
}
}
// Check diagonals
if (board[0][0] === player && board[1][1] === player && board[2][2] === player) {
return true;
}
if (board[0][2] === player && board[1][1] === player && board[2][0] === player) {
return true;
}
return false;
};
const xWins = checkWin('X');
const oWins = checkWin('O');
// Rule 2: If X wins, the number of X's must be one more than O's.
// If xCount == oCount, it implies O just made a move, which is impossible if X has already won.
if (xWins && xCount === oCount) {
return false;
}
// Rule 3: If O wins, the number of X's must be equal to O's.
// If xCount > oCount, it implies X just made a move, which is impossible if O has already won.
if (oWins && xCount > oCount) {
return false;
}
// Note: The case where both win is implicitly handled.
return true;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.