Is This Tic-Tac-Toe Board Reachable
Updated
Problem statement
Two players, X and O, play tic-tac-toe on a 3 x 3 board. They take turns, and on each turn a player puts one of their own marks on an empty cell. The player named by first makes the first move. The game ends as soon as one player has three of their marks in a row, a column or a diagonal; nobody moves after that. It also ends when the board is full.
The board is given as three strings, one per row from top to bottom. Each character is 'X', 'O' or '.', where '.' is an empty cell.
Return true if this board can appear at some moment of such a game (the empty board before the first move counts), and false otherwise.
Function
isReachable(board: String[], first: String) → booleanExamples
Example 1
board = ["X.O",".X.","..."]first = "X"return = trueX has 2 marks and O has 1, and nobody has three in a row. X moved, then O, then X: this board can happen.
Example 2
board = ["X.O",".X.","..."]first = "O"return = falseThe same board, but now O moves first. Then O always has as many marks as X or one more, never fewer. Here O has 1 and X has 2, so the board cannot happen.
Example 3
board = ["XXX","OO.","..O"]first = "X"return = falseX completed the top row with its third mark. The game ends at that moment, so O could not have placed a third mark afterwards. With 3 marks each, O must have moved after X won: impossible.
Constraints
board.length == 3andboard[i].length == 3- Each
board[i][j]is'X','O'or'.' firstis"X"or"O"