Skip to content
BASA DSA

Surrounded Regions

MediumGraphs

Mutate a board by replacing every O component not connected orthogonally to a boundary O with X. Boundary-connected regions remain O. Return nothing.

Example 1

0,0X
0,1X
0,2X
0,3X
1,0X
1,1O
1,2O
1,3X
2,0X
2,1X
2,2O
2,3X
3,0X
3,1O
3,2X
3,3X
X = boundary; O = open region. Border-connected O cells are preserved.
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"]]

Explanation

The enclosed interior three-cell O region becomes X; the bottom-boundary O survives.

Example 2

0,0O
0,1O
1,0O
1,1O
X = boundary; O = open region. Border-connected O cells are preserved.
Input
board = [["O","O"],["O","O"]]
Output
[["O","O"],["O","O"]]

Explanation

Every cell touches a boundary on this 2-by-2 board, so no O is captured.

Example 3

0,0X
X = boundary; O = open region. Border-connected O cells are preserved.
Input
board = [["X"]]
Output
[["X"]]

Explanation

There is no O region to change.

Constraints

  • 1 <= rows, columns <= 200
  • Cells are O or X

Function contractsolveArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…