#794

Valid Tic-Tac-Toe State

expert · 1135 · lc medium +32 · verified · 34.8% accepted · 587 likes · top 12%

Description

Given a Tic-Tac-Toe board represented as a string array board, return true if and only if this board state is reachable through a sequence of valid moves in a Tic-Tac-Toe game.

The board is a 3 x 3 grid containing the characters ' ', 'X', and 'O', where ' ' denotes an empty cell.

The rules are:

- Players alternate placing their character into an empty ' ' cell.

- The first player always uses 'X'; the second always uses 'O'.

- Characters may only be placed into empty cells, never occupied ones.

- The game ends when three identical non-empty characters occupy any row, column, or diagonal.

- The game also ends when no empty cells remain.

- No moves may be made after the game has ended.

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

Code

1
2
3