Skip to content

Practice Surrounded Regions

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#130
LeetCode Problem

Surrounded Regions

Problem Statement

In the board of X and O cells, connect O cells using horizontal or vertical neighbors only. A connected group is surrounded when none of its cells touches the outer border. Change all O cells in such groups to X in the original board. Leave border-connected O groups unchanged. The operation is in place and does not require a return value.

Source: Surrounded Regions on LeetCode. Use the original task for its coding environment and submissions.

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

# Example 2

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

Constraints

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

Related LeetCode Problems

Practice more problems similar to Surrounded Regions.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.