Skip to content
Onsite

Surrounded Regions

MediumP3Coding

A board holds the characters "X" and "O". A region is a group of "O" cells connected up, down, left or right. Capture every region that is completely surrounded by "X", meaning no cell of the region is on the border of the board: flip all its "O" cells to "X". Regions that touch the border, and any "O" connected to one, stay as they are.

Modify board in place; the function returns nothing.

Example 1

Input
board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]]
Output
[["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]
Why
The bottom O is on the border and stays; the other three are enclosed.

Example 2

Input
board = [["X"]]
Output
[["X"]]

Constraints

  • 1 <= rows, cols <= 200
  • board[i][j] is "X" or "O"

Hints

Run checks the 3 visible tests, Submit all 8.