cheetcode

84. Surrounded RegionsMedium

Capture regions that are fully surrounded and preserve regions connected to the border.

Given a board containing X and O, flip every O that is completely surrounded by X into X.

Border-connected O cells stay unchanged. Return the transformed board.

Model the relationships explicitly, then choose BFS, DFS, or union find based on the question. Track visited state so cycles do not create repeated work.

Examples

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"]]
Input: board = [["X"]]
Output: [["X"]]

Constraints

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