130. Surrounded Regions
Medium NeetCode 150 ↗ Open on LeetCode Source: NeetCode 150
Quick review
- Pattern
- Graphs
- Time
- O(M*N)
- Space
- O(M*N)
Standard Solution (C++)
SummaryGiven an m x n grid of 'X' and 'O' characters, flip to 'X' every 'O' cell that is fully surrounded by 'X's (i.e., cannot reach the board's border through adjacent 'O' cells), leaving all border-connected 'O' regions unchanged.
Approach
- Key insight: Any 'O' connected (4-directionally) to a border cell can never be surrounded, so only interior 'O's that have no path to the border should flip.
- Start a BFS/DFS from every 'O' on the four edges, marking all reachable 'O's as safe (temporary sentinel).
- After the traversal, any unmarked 'O' is truly surrounded — flip it to 'X'; restore sentinel cells back to 'O'.
- This runs in a single BFS pass plus one linear scan, touching each cell at most twice.
#include <vector>
#include <queue>
using namespace std;
class Solution {
public:
void solve(vector<vector<char>>& board) {
const int m = board.size();
const int n = board[0].size();
const int dirs[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
// BFS helper: mark all 'O' cells reachable from (r,c) with a sentinel
auto bfs = [&](int r, int c) {
if (board[r][c] != 'O') return;
queue<pair<int,int>> q;
board[r][c] = '#'; // sentinel = safe
q.push({r, c});
while (!q.empty()) {
auto [x, y] = q.front(); q.pop();
for (auto& d : dirs) {
int nx = x + d[0], ny = y + d[1];
if (nx >= 0 && nx < m && ny >= 0 && ny < n && board[nx][ny] == 'O') {
board[nx][ny] = '#';
q.push({nx, ny});
}
}
}
};
// Seed BFS from all border 'O' cells
for (int j = 0; j < n; ++j) { bfs(0, j); bfs(m-1, j); }
for (int i = 1; i < m-1; ++i) { bfs(i, 0); bfs(i, n-1); }
// Final pass: flip surrounded 'O' -> 'X', restore sentinels -> 'O'
for (auto& row : board)
for (char& c : row)
c = (c == '#') ? 'O' : (c == 'O') ? 'X' : c;
}
};
Time O(m * n) Space O(m * n)
A canonical reference solution. Problem statement © LeetCode — see the official link above.
My Solution (C++)
class Solution {
public:
void solve(vector<vector<char>>& board) {
const int m=board.size();
const int n=board[0].size();
queue<pair<int, int>> q;
for(int j=0; j<n; ++j){
if(board[0][j]=='O'){
q.push({0, j});
board[0][j]='#';
}
if(board[m-1][j]=='O'){
q.push({m-1, j});
board[m-1][j]='#';
}
}
for(int i=1; i<m-1; ++i){
if(board[i][0]=='O'){
q.push({i, 0});
board[i][0]='#';
}
if(board[i][n-1]=='O'){
q.push({i, n-1});
board[i][n-1]='#';
}
}
const int dirs[4][2]={{-1,0}, {1,0}, {0,-1}, {0,1}};
while(!q.empty()){
auto [x, y]=q.front();
q.pop();
for(auto& d: dirs){
int nx=x+d[0];
int ny=y+d[1];
if(nx>0 && nx<m-1 && ny>0 && ny<n-1 && board[nx][ny]=='O'){
board[nx][ny]='#';
q.push({nx, ny});
}
}
}
for(auto& row: board){
for(char& c: row){
if(c=='#'){
c='O';
}
else if(c=='O'){
c='X';
}
}
}
}
};
// Time Complexity: O(M*N)
// Space Complexity: O(M*N)
Time O(M*N) Space O(M*N)
My own solution and notes, as I wrote them.
▶ Run it — editable in-browser playground with a sample driver C++
Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.
C++ ▶ Runnable Auto-generated driver with sample input — unverified.
Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.