304 North Cardinal St.
Dorchester Center, MA 02124

# Surrounded Regions LeetCode Solution – Queslers

## Problem – Surrounded Regions

Given an `m x n` matrix `board` containing `'X'` and `'O'`capture all regions that are 4-directionally surrounded by `'X'`.

A region is captured by flipping all `'O'`s into `'X'`s in that surrounded region.

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"]]
Explanation: Notice that an 'O' should not be flipped if:
- It is on the border, or
- It is adjacent to an 'O' that should not be flipped.
The bottom 'O' is on the border, so it is not flipped.
The other three 'O' form a surrounded region, so they are flipped.``````

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'`.

### Surrounded Regions LeetCode Solution in C++

``````class Solution {
public:
void solve(vector<vector<char>>& board) {
int i,j;
int row=board.size();
if(!row)
return;
int col=board.size();

for(i=0;i<row;i++){
check(board,i,0,row,col);
if(col>1)
check(board,i,col-1,row,col);
}
for(j=1;j+1<col;j++){
check(board,0,j,row,col);
if(row>1)
check(board,row-1,j,row,col);
}
for(i=0;i<row;i++)
for(j=0;j<col;j++)
if(board[i][j]=='O')
board[i][j]='X';
for(i=0;i<row;i++)
for(j=0;j<col;j++)
if(board[i][j]=='1')
board[i][j]='O';
}
void check(vector<vector<char> >&vec,int i,int j,int row,int col){
if(vec[i][j]=='O'){
vec[i][j]='1';
if(i>1)
check(vec,i-1,j,row,col);
if(j>1)
check(vec,i,j-1,row,col);
if(i+1<row)
check(vec,i+1,j,row,col);
if(j+1<col)
check(vec,i,j+1,row,col);
}
}
};
``````

### Surrounded Regions LeetCode Solution in Python

``````def solve(self, board):
if not any(board): return

m, n = len(board), len(board)
save = [ij for k in range(m+n) for ij in ((0, k), (m-1, k), (k, 0), (k, n-1))]
while save:
i, j = save.pop()
if 0 <= i < m and 0 <= j < n and board[i][j] == 'O':
board[i][j] = 'S'
save += (i, j-1), (i, j+1), (i-1, j), (i+1, j)

board[:] = [['XO'[c == 'S'] for c in row] for row in board]``````

### Surrounded Regions LeetCode Solution in Java

``````public void solve(char[][] board) {
if (board.length == 0 || board.length == 0)
return;
if (board.length < 2 || board.length < 2)
return;
int m = board.length, n = board.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] == '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[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';
}
}
}
//Use DFS algo to turn internal however boundary-connected 'O' to '*';
private void boundaryDFS(char[][] board, int i, int j) {
if (i < 0 || i > board.length - 1 || j <0 || j > board.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 < board.length - 2 && board[i+1][j] == 'O')
boundaryDFS(board, i+1, j);
if (j > 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);
}
``````
##### Surrounded Regions LeetCode Solution Review:

In our experience, we suggest you solve this Surrounded Regions LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Surrounded Regions LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Surrounded Regions LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions