Surrounded Regions

You are given an m x n matrix board containing letters 'X' and 'O'. Capture regions that are surrounded.

  • Connect: A cell is connected to adjacent cells horizontally or vertically.
  • Region: To form a region, connect every 'O' cell.
  • Surround: A region is surrounded if none of the 'O' cells in that region are on the edge of the board. Such regions are completely enclosed by 'X' cells.

To capture a surrounded region, replace all 'O's with 'X's in-place within the original board. You do not need to return anything.

Example 1
X X X X
X O O X
X X O X
X O X X
Inputboard = [["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 bottom region is not captured because it is on the edge of the board and cannot be surrounded.
Example 2
X
Inputboard = [["X"]]
Output[["X"]]
The single cell is already an 'X', so the board remains unchanged.

Constraints

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

Asked at 11 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate