Ctrl K

Reachable Noughts and Crosses Position

Medium
A 3×3 noughts and crosses board is given as a list of three strings rows. Each character is 'X', 'O' or a space ' ' for an empty cell. The rules: - players take turns placing their own mark in an empty cell; - the 'X' player always moves first, the 'O' player second; - the game ends as soon as three identical marks fill a row, a column or a diagonal, or when no empty cells remain. No moves are made after that. Return True if this position can come up in a fair game played by these rules, and False otherwise.
Example 1
rows=['XX ', 'OO ', '   '] → True

X has made two moves and O has made two, so it is X's turn. This position can happen.

Example 2
rows=['OOO', 'XX ', 'XX '] → False

O completed a row, yet X has more marks: once O won, X could not have moved again.

Example 3
rows=['  X', '   ', '  X'] → False

Two X marks and no O marks: players alternate, so this cannot happen.

Constraints
  • len(rows) == 3
  • len(rows[i]) == 3
  • rows[i][j] is 'X', 'O' or ' '
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSudoku Board Without Clashes MediumTurn a Square Clockwise MediumSpiral Readout MediumSpiral-Numbered Square MediumWarehouse Cart Routes Medium
loading Python…
Input
rows=['XX ', 'OO ', '   ']
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 93 tests on the server, hidden ones included.