LeetCode – Surrounded Regions (Java)

Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region.

For example,

```X X X X
X O O X
X X O X
X O X X
```

After running your function, the board should be:

```X X X X
X X X X
X X X X
X O X X
```

Analysis

This problem is similar to Number of Islands. In this problem, only the cells on the boarders can not be surrounded. So we can first merge those O's on the boarders like in Number of Islands and replace O's with '#', and then scan the board and replace all O's left (if any).

1. Depth-first Search

```public void solve(char[][] board) { if(board == null || board.length==0) return;   int m = board.length; int n = board[0].length;   //merge O's on left & right boarder for(int i=0;i<m;i++){ if(board[i][0] == 'O'){ merge(board, i, 0); }   if(board[i][n-1] == 'O'){ merge(board, i,n-1); } }   //merge O's on top & bottom boarder for(int j=0; j<n; j++){ if(board[0][j] == 'O'){ merge(board, 0,j); }   if(board[m-1][j] == 'O'){ merge(board, m-1,j); } }   //process the board 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] == '#'){ board[i][j] = 'O'; } } } }   public void merge(char[][] board, int i, int j){ if(i<0 || i>=board.length || j<0 || j>=board[0].length) return;   if(board[i][j] != 'O') return;   board[i][j] = '#';   merge(board, i-1, j); merge(board, i+1, j); merge(board, i, j-1); merge(board, i, j+1); }```

This solution causes java.lang.StackOverflowError, because for a large board, too many method calls are pushed to the stack and causes the overflow.

2. Breath-first Search

We can also use a queue to do breath-first search for this problem.

```public void solve(char[][] board) { if(board==null || board.length==0 || board[0].length==0) return;   int m=board.length; int n=board[0].length;     for(int j=0; j<n; j++){ if(board[0][j]=='O'){ bfs(board, 0, j); } }   for(int j=0; j<n; j++){ if(board[m-1][j]=='O'){ bfs(board, m-1, j); } }   for(int i=0; i<m; i++){ if(board[i][0]=='O'){ bfs(board, i, 0); } }   for(int i=0; i<m; i++){ if(board[i][n-1]=='O'){ bfs(board, i, n-1); } }   for(int i=0; i<m; i++){ for(int j=0; j<n; j++){ if(board[i][j]=='O'){ board[i][j]='X'; } if(board[i][j]=='1'){ board[i][j]='O'; } } } }   public void bfs(char[][] board, int o, int p){ int m=board.length; int n=board[0].length;   int index = o*n+p; LinkedList<Integer> queue = new LinkedList<Integer>(); queue.offer(index); board[o][p]='1';   while(!queue.isEmpty()){ int top = queue.poll(); int i=top/n; int j=top%n;   if(i-1>=0 && board[i-1][j]=='O'){ board[i-1][j]='1'; queue.offer((i-1)*n+j); } if(i+1<m && board[i+1][j]=='O'){ board[i+1][j]='1'; queue.offer((i+1)*n+j); } if(j-1>=0 && board[i][j-1]=='O'){ board[i][j-1]='1'; queue.offer(i*n+j-1); } if(j+1<n && board[i][j+1]=='O'){ board[i][j+1]='1'; queue.offer(i*n+j+1); } } }```
Category >> Algorithms >> Interview >> Java
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
```<pre><code>
String foo = "bar";
</code></pre>
```
1. Shalom Ray on 2016-2-12

What is the time complexity of the BFS program?

2. Kyle on 2016-3-19

DFS gives stackoverflow

3. vivek on 2017-2-11

I feel stackoverflow error comes because we end up visiting the same index again.

4. Anonymous on 2017-6-10

To avoid stackoverflow error, avoid revisiting the boundaries again. This is done by using the if conditions. Below is the code (5 milli secs run time.)

public class Solution {
public void solve(char[][] board) {
if (board.length == 0 || board[0].length == 0)
return;
if (board.length < 2 || board[0].length < 2)
return;
int m = board.length, n = board[0].length;
//Any 'O' connected to a boundary can't be turned to 'X', so …
//Start from first and last column, turn 'O' to '*'.
for (int i = 0; i < m; i++) {
if (board[i][0] == 'O')
boundaryDFS(board, i, 0);
if (board[i][n-1] == 'O')
boundaryDFS(board, i, n-1);
}
//Start from first and last row, turn '0' to '*'
for (int j = 0; j < n; j++) {
if (board[0][j] == 'O')
boundaryDFS(board, 0, j);
if (board[m-1][j] == 'O')
boundaryDFS(board, m-1, j);
}
//post-prcessing, turn 'O' to 'X', '*' back to 'O', keep 'X' intact.
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] == '*')
board[i][j] = 'O';
}
}
}

private void boundaryDFS(char[][] board, int i, int j) {
if (i board.length – 1 || j board[0].length – 1)
return;
if (board[i][j] == ‘O’)
board[i][j] = ‘*’;
if (i > 1 && board[i-1][j] == ‘O’)
boundaryDFS(board, i-1, j);
if (i 1 && board[i][j-1] == ‘O’)
boundaryDFS(board, i, j-1);
if (j < board[i].length – 2 && board[i][j+1] == 'O' )
boundaryDFS(board, i, j+1);
}
}