'O' that are completely surrounded by 'X' on a 2D board.
The goal is to replace all surrounded
'O' regions with 'X' while preserving any 'O' that is connected to the boundary of the board.
Problem
You are given anm x n matrix board containing the characters 'X' and 'O'. Capture all regions that are surrounded:
- A region consists of connected cells containing
'O'. Cells are connected horizontally or vertically.- A region is surrounded if none of its cells are on the boundary of the board and it is not connected to any boundary cell containing
'O'.- To capture a surrounded region, replace all its
'O' cells with 'X'.
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"]
]
Solution
This problem can be solved using a Depth-First Search (DFS) approach. Instead of identifying every surrounded region directly, we first identify all'O' cells that cannot be captured.
The key observation is that an
'O' connected to a boundary 'O' cannot be surrounded. Therefore, we must preserve every 'O' that is reachable from the boundary.
First, we traverse all boundary cells. Whenever we find an
'O', we start a DFS from that cell and temporarily mark every connected 'O' as a special character, such as 'T'. This marks all cells that must be preserved.
Next, we traverse the entire board and process each cell: - If the cell contains
'O', it was not reachable from the boundary, so we replace it with 'X'.- If the cell contains
'T', it is connected to the boundary, so we restore it to 'O'.- Cells containing
'X' remain unchanged.
This approach works because every region connected to the boundary is preserved, while every remaining
'O' belongs to a surrounded region and can safely be captured.
class Solution {
public void solve(char[][] board) {
int m = board.length;
int n = board[0].length;
// Check the first and last columns
for (int i = 0; i < m; i++) {
dfs(board, i, 0);
dfs(board, i, n - 1);
}
// Check the first and last rows
for (int j = 0; j < n; j++) {
dfs(board, 0, j);
dfs(board, m - 1, j);
}
// Capture surrounded regions and restore safe regions
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == 'O') {
board[i][j] = 'X';
} else if (board[i][j] == 'T') {
board[i][j] = 'O';
}
}
}
}
private void dfs(char[][] board, int i, int j) {
int m = board.length;
int n = board[0].length;
// Stop if out of bounds or not an unvisited O
if (i < 0 || i >= m ||
j < 0 || j >= n ||
board[i][j] != 'O') {
return;
}
// Mark this cell as safe
board[i][j] = 'T';
// Explore all four directions
dfs(board, i + 1, j);
dfs(board, i - 1, j);
dfs(board, i, j + 1);
dfs(board, i, j - 1);
}
}
Complexity
Letm be the number of rows and n be the number of columns.
We visit the boundary cells and perform DFS to mark all reachable
'O' cells. Each cell is visited at most once by DFS. We then traverse the board once more to capture surrounded regions and restore safe cells. Therefore, the overall time complexity is O(m x n).
The recursive DFS can use up to
O(m x n) space on the call stack in the worst case, when a large number of cells are connected. The board is modified in place, so no additional matrix is required.
Comments
No comments yet. Ask a question or share what helped.