794. Valid Tic-Tac-Toe State #
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.
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 = ["XXX"," ","OOO"]
Output: false
Example 4:

Input: board = ["XOX","O O","XOX"]
Output: true
Constraints:
- board.length == 3
- board[i].length == 3
- board[i][j] is either ‘X’, ‘O’, or ' ‘.
Problem Summary #
Given a string array board representing a Tic-Tac-Toe board. Return true if and only if, during the course of a Tic-Tac-Toe game, it is possible for the board to reach the state shown by board.
The Tic-Tac-Toe board is a 3 x 3 array consisting of the 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 (’ ‘).
- Player 1 always places the character ‘X’, while Player 2 always places the character ‘O’.
- ‘X’ and ‘O’ may only be placed in empty squares; filled squares cannot be filled again.
- The game ends when 3 identical (and non-empty) characters fill any row, column, or diagonal.
- The game also ends when all squares are non-empty.
- If the game is over, players are not allowed to place any more characters.
Solution Ideas #
Classification simulation:
- According to the problem statement, at any time on the board, either the number of X’s is one more than the number of O’s, or the two numbers are equal
- When the number of X’s equals the number of O’s, there must not be 3 identical X’s in any row, column, or diagonal
- When the number of X’s is one more than the number of O’s, there must not be 3 identical O’s in any row, column, or diagonal
Code #
package leetcode
func validTicTacToe(board []string) bool {
cntX, cntO := 0, 0
for i := range board {
for j := range board[i] {
if board[i][j] == 'X' {
cntX++
} else if board[i][j] == 'O' {
cntO++
}
}
}
if cntX < cntO || cntX > cntO+1 {
return false
}
if cntX == cntO {
return process(board, 'X')
}
return process(board, 'O')
}
func process(board []string, c byte) bool {
//A certain row is "ccc"
if board[0] == string([]byte{c, c, c}) || board[1] == string([]byte{c, c, c}) || board[2] == string([]byte{c, c, c}) {
return false
}
//A certain column is "ccc"
if (board[0][0] == c && board[1][0] == c && board[2][0] == c) ||
(board[0][1] == c && board[1][1] == c && board[2][1] == c) ||
(board[0][2] == c && board[1][2] == c && board[2][2] == c) {
return false
}
//A certain diagonal is "ccc"
if (board[0][0] == c && board[1][1] == c && board[2][2] == c) ||
(board[0][2] == c && board[1][1] == c && board[2][0] == c) {
return false
}
return true
}