Skip to content
AI360Xpert
Beta
Difficulty: MediumGraphs

Surrounded Regions

Problem in Plain English

Flip every region of O cells that is fully enclosed by X cells into X, while leaving any O connected to the board edge untouched. The board is modified in place and nothing is returned.

Problem Statement

You are given an m x n matrix board holding 'X' and 'O'. Capture every region that is 4-directionally surrounded by 'X' by flipping all of its 'O' cells into 'X' cells.

Any 'O' cell connected to the border of the board, directly or through other 'O' cells, escapes capture. Modify the board in place and do not return anything.

Constraints

  • m == board.length
  • n == board[i].length
  • 1 <= m, n <= 200
  • board[i][j] is 'X' or 'O'.

Examples

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"]]

The border (3,1) escapes capture and stays O, while the inland cells (1,1), (1,2) and (2,2) are surrounded and flip to X.

Example 2

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

The single cell is already X, so nothing changes.

Example 3

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

The corner O remains because it is on the border. The center O touches it only diagonally, so no allowed path connects them; all four side neighbours of the center are X and it is captured.

Example 4

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

The inland O at (1,1) reaches the top edge through (0,1), so both cells remain O. A cell need not itself lie on the border to escape.

Intuition

A region is a connected component of O cells joined by shared sides. It escapes capture exactly when some path in that component reaches a board edge; diagonal contact does not help. Example 1 has a three-cell inland component and the separate border cell (3,1). Testing each component for an escape works, but safe components may be traversed repeatedly. Reverse the question: start at border O cells, mark everything reachable as safe, and capture only what remains. Use an explicit stack so a 200 by 200 region does not exhaust the language call stack.

Approaches

Region Check Per Cell

Solution Details

Reveal Region Check Per Cell: intuition, complexity, and code

Hints

Hint 1
A border cell can never be surrounded, so every region touching the border is safe by definition.
Hint 2
A safe region spreads through connected cells, so one flood from the borders marks every escapee, not just the border cells themselves.
Hint 3
Stamp the escapees with a temporary mark, flip every unmarked cell, then wash the stamps back to their original value.

Edge Cases

  • If either dimension is at most 2, every cell lies on a border, so nothing can be captured. This includes a 2 by 200 board; a vague comparison with 3 by 3 is insufficient.
  • An all-X board is unchanged. An all-O board is also unchanged because every cell connects to a border.
  • A single inland O surrounded by four X neighbours is captured, even if it touches a border O diagonally.
  • A winding O path to any edge protects its entire component, including cells far from the border.
  • At the 200 by 200 maximum, large enclosed components are captured and large escaping components survive without recursive stack overflow.
  • Both approaches modify existing rows, return nothing, remove all temporary marks, and are idempotent: a second call leaves the final board unchanged.

Common Mistakes and Interview Tips

  • Using diagonal neighbours falsely protects the center in Example 3. Only four side directions belong to the graph.
  • Flipping cells before determining escape can destroy a path to the border. The baseline defers all changes; the optimal approach first marks all safe cells.
  • Marking only border cells misses interior members of escaping components. Traverse their entire O component.
  • Leaving T marks in the output violates the board contract. Restore them with an else-if branch after capture decisions.
  • Calling input marking constant-space ignores the explicit stack. Recursive DFS can fail on long 200 by 200 regions; use the published iterative code.
  • Interview tip: an inland location alone does not imply capture. State the equivalence between escape and a path to a border O before explaining the reverse traversal.

Key Takeaway

When only the components sealed off from the boundary matter, flood inward from the boundary once and treat everything untouched as sealed.