Surrounded Regions
Difficulty: Medium
You're given a grid of "X" and "O" characters. A group of connected "O"s that doesn't touch the grid's border is surrounded by "X"s and gets captured: flip every "O" in it to "X".
Groups that touch the border (even by one cell) are safe and stay "O". Modify the grid in place.
Examples
Input: board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]]
Output: board = [["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]
The connected group of three O's in the middle never touches a border cell, so it's captured. The lone O in the bottom row is itself on the border, so it survives.
Input: board = [["X"]]
Output: board = [["X"]]
There are no O's, so nothing changes.
Constraints
1 <= rows, cols <= 200
Approach
Testing each group directly for "surrounded" is awkward. Flip it: find every safe O (connected to the border, directly or through other O's), then capture the rest.
Start a traversal from every "O" on the border and spread inward through connected "O"s. None of these can be captured. Mark each with a placeholder (like "#") to tell it apart from unvisited "O"s.
Then make one final pass:
- A plain "O" was never reached from the border, so capture it (flip to "X").
- A placeholder was safe, so flip it back to "O".
Solutions
DFS - Recursive Border Flood Fill
Recursively spread from every border "O", marking each cell reached as safe. Then sweep the grid: unmarked O's are captured, and marked cells go back to O.
function solve(board) {
const rows = board.length;
const cols = board[0].length;
function dfs(r, c) {
if (r < 0 || r >= rows || c < 0 || c >= cols || board[r][c] !== "O") {
return;
}
board[r][c] = "#"; // mark safe
dfs(r + 1, c);
dfs(r - 1, c);
dfs(r, c + 1);
dfs(r, c - 1);
}
for (let r = 0; r < rows; r++) {
dfs(r, 0);
dfs(r, cols - 1);
}
for (let c = 0; c < cols; c++) {
dfs(0, c);
dfs(rows - 1, c);
}
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (board[r][c] === "O") {
board[r][c] = "X";
} else if (board[r][c] === "#") {
board[r][c] = "O";
}
}
}
return board;
}Time: O(rows × cols) · Space: O(rows × cols)
BFS - Iterative Border Flood Fill
Same idea without recursion: queue every border "O" up front and expand from all of them together.
function solve(board) {
const rows = board.length;
const cols = board[0].length;
const queue = [];
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
const onBorder = r === 0 || r === rows - 1 || c === 0 || c === cols - 1;
if (onBorder && board[r][c] === "O") {
board[r][c] = "#";
queue.push([r, c]);
}
}
}
while (queue.length > 0) {
const [r, c] = queue.shift();
const neighbors = [[r + 1, c], [r - 1, c], [r, c + 1], [r, c - 1]];
for (const [nr, nc] of neighbors) {
if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && board[nr][nc] === "O") {
board[nr][nc] = "#";
queue.push([nr, nc]);
}
}
}
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (board[r][c] === "O") {
board[r][c] = "X";
} else if (board[r][c] === "#") {
board[r][c] = "O";
}
}
}
return board;
}Time: O(rows × cols) · Space: O(rows × cols)