130. Surrounded Regions
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to capture regions of 'O's that are completely surrounded by 'X's on a 2D board. An 'O' is part of a surrounded region if it's not on the border of the board and cannot reach the border by moving through other 'O's.
Solution
Surrounded Regions in Python
class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
if not board or not board[0]:
return
m, n = len(board), len(board[0])
def dfs(r, c):
# Check boundaries and if the cell is not an 'O'.
if not (0 <= r < m and 0 <= c < n and board[r][c] == 'O'):
return
# Mark the current cell as safe.
board[r][c] = '#'
# Explore neighbors.
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
# Step 1: Mark 'O's on the border and any 'O's connected to them.
# We use a temporary marker '#' for 'O's that should not be flipped.
# Traverse the first and last rows.
for j in range(n):
if board[0][j] == 'O':
dfs(0, j)
if board[m - 1][j] == 'O':
dfs(m - 1, j)
# Traverse the first and last columns.
for i in range(m):
if board[i][0] == 'O':
dfs(i, 0)
if board[i][n - 1] == 'O':
dfs(i, n - 1)
# Step 2: Flip the remaining 'O's to 'X's and revert '#' back to 'O'.
for i in range(m):
for j in range(n):
if board[i][j] == 'O':
board[i][j] = 'X' # These 'O's are surrounded.
elif board[i][j] == '#':
board[i][j] = 'O' # These are the safe 'O's.
Surrounded Regions in Java
class Solution {
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;
// Step 1: Mark 'O's on the border and any 'O's connected to them.
// We use a temporary marker '#' for 'O's that should not be flipped.
// Traverse the first and last rows.
for (int j = 0; j < n; j++) {
if (board[0][j] == 'O') {
dfs(board, 0, j);
}
if (board[m - 1][j] == 'O') {
dfs(board, m - 1, j);
}
}
// Traverse the first and last columns.
for (int i = 0; i < m; i++) {
if (board[i][0] == 'O') {
dfs(board, i, 0);
}
if (board[i][n - 1] == 'O') {
dfs(board, i, n - 1);
}
}
// Step 2: Flip the remaining 'O's to 'X's and revert '#' back to 'O'.
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == 'O') {
board[i][j] = 'X'; // These 'O's are surrounded.
} else if (board[i][j] == '#') {
board[i][j] = 'O'; // These are the safe 'O's.
}
}
}
}
private void dfs(char[][] board, int r, int c) {
int m = board.length;
int n = board[0].length;
// Check boundaries and if the cell is not an 'O'.
if (r < 0 || r >= m || c < 0 || c >= n || board[r][c] != 'O') {
return;
}
// Mark the current cell as safe.
board[r][c] = '#';
// Explore neighbors.
dfs(board, r + 1, c);
dfs(board, r - 1, c);
dfs(board, r, c + 1);
dfs(board, r, c - 1);
}
}
Surrounded Regions in C++
class Solution {
public:
void solve(vector<vector<char>>& board) {
if (board.empty() || board[0].empty()) return;
int m = board.size();
int n = board[0].size();
// Step 1: Mark 'O's on the border and any 'O's connected to them.
// We use a temporary marker '#' for 'O's that should not be flipped.
// Traverse the first and last rows.
for (int j = 0; j < n; ++j) {
if (board[0][j] == 'O') {
dfs(board, 0, j);
}
if (board[m - 1][j] == 'O') {
dfs(board, m - 1, j);
}
}
// Traverse the first and last columns.
for (int i = 0; i < m; ++i) {
if (board[i][0] == 'O') {
dfs(board, i, 0);
}
if (board[i][n - 1] == 'O') {
dfs(board, i, n - 1);
}
}
// Step 2: Flip the remaining 'O's to 'X's and revert '#' back to 'O'.
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (board[i][j] == 'O') {
board[i][j] = 'X'; // These 'O's are surrounded.
} else if (board[i][j] == '#') {
board[i][j] = 'O'; // These are the safe 'O's.
}
}
}
}
private:
void dfs(vector<vector<char>>& board, int r, int c) {
int m = board.size();
int n = board[0].size();
// Check boundaries and if the cell is not an 'O'.
if (r < 0 || r >= m || c < 0 || c >= n || board[r][c] != 'O') {
return;
}
// Mark the current cell as safe.
board[r][c] = '#';
// Explore neighbors.
dfs(board, r + 1, c);
dfs(board, r - 1, c);
dfs(board, r, c + 1);
dfs(board, r, c - 1);
}
};
Surrounded Regions in TypeScript
/**
Do not return anything, modify board in-place instead.
*/
function solve(board: string[][]): void {
if (!board || board.length === 0 || board[0].length === 0) {
return;
}
const m = board.length;
const n = board[0].length;
const dfs = (r: number, c: number): void => {
// Check boundaries and if the cell is not an 'O'.
if (r < 0 || r >= m || c < 0 || c >= n || board[r][c] !== 'O') {
return;
}
// Mark the current cell as safe.
board[r][c] = '#';
// Explore neighbors.
dfs(r + 1, c);
dfs(r - 1, c);
dfs(r, c + 1);
dfs(r, c - 1);
};
// Step 1: Mark 'O's on the border and any 'O's connected to them.
// We use a temporary marker '#' for 'O's that should not be flipped.
// Traverse the first and last rows.
for (let j = 0; j < n; j++) {
if (board[0][j] === 'O') {
dfs(0, j);
}
if (board[m - 1][j] === 'O') {
dfs(m - 1, j);
}
}
// Traverse the first and last columns.
for (let i = 0; i < m; i++) {
if (board[i][0] === 'O') {
dfs(i, 0);
}
if (board[i][n - 1] === 'O') {
dfs(i, n - 1);
}
}
// Step 2: Flip the remaining 'O's to 'X's and revert '#' back to 'O'.
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (board[i][j] === 'O') {
board[i][j] = 'X'; // These 'O's are surrounded.
} else if (board[i][j] === '#') {
board[i][j] = 'O'; // These are the safe 'O's.
}
}
}
}
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.