30. Replace O's with X's
โ GFG solution to the Replace O's with X's problem: replace all 'O' surrounded by 'X' using boundary traversal and DFS/BFS techniques. ๐
The problem can be found at the following link: ๐ Question Link
๐งฉ Problem Description
You are given a grid[][] of size n*m, where every element is either 'O' or 'X'. Your task is to replace all 'O' or a group of 'O' with 'X' that are surrounded by 'X'.
A 'O' (or a set of 'O') is considered to be surrounded by 'X' if there are 'X' at locations just below, just above, just left and just right of it.
๐ Examples
Example 1
Input:
grid[][] = [['X', 'X', 'X', 'X'],
['X', 'O', 'X', 'X'],
['X', 'O', 'O', 'X'],
['X', 'O', 'X', 'X'],
['X', 'X', 'O', 'O']]
Output:
[['X', 'X', 'X', 'X'],
['X', 'X', 'X', 'X'],
['X', 'X', 'X', 'X'],
['X', 'X', 'X', 'X'],
['X', 'X', 'O', 'O']]
Explanation: We only changed those 'O' that are surrounded by 'X'Example 2
Input:
grid[][] = [['X', 'O', 'X', 'X'],
['X', 'O', 'X', 'X'],
['X', 'O', 'O', 'X'],
['X', 'O', 'X', 'X'],
['X', 'X', 'O', 'O']]
Output:
[['X', 'O', 'X', 'X'],
['X', 'O', 'X', 'X'],
['X', 'O', 'O', 'X'],
['X', 'O', 'X', 'X'],
['X', 'X', 'O', 'O']]
Explanation: There's no 'O' that's surround by 'X'.Example 3
Input:
grid[][] = [['X', 'X', 'X'],
['X', 'O', 'X'],
['X', 'X', 'X']]
Output:
[['X', 'X', 'X'],
['X', 'X', 'X'],
['X', 'X', 'X']]
Explanation: There's only one 'O' that's surround by 'X'.๐ Constraints
$1 \le \text{grid.size()} \le 100$
$1 \le \text{grid[0].size()} \le 100$
โ
My Approach
The optimal approach uses boundary traversal with DFS/BFS to identify 'O' cells that are NOT surrounded:
Boundary DFS Marking
Mark Safe Regions:
Replace all 'O' cells with temporary marker '-' throughout the grid.
Any 'O' connected to boundary cannot be surrounded.
Traverse Boundaries:
Start DFS/BFS from all boundary cells containing '-'.
Mark all connected '-' cells back to 'O' as they are safe from replacement.
Process Four Edges:
Check top row, bottom row, left column, and right column.
For each '-' found on boundary, recursively mark all connected regions.
Final Replacement:
All remaining '-' cells are surrounded, so replace them with 'X'.
Cells marked as 'O' in step 2 remain unchanged.
Key Insight:
Instead of finding surrounded regions directly, find regions that touch boundaries.
Everything else is automatically surrounded.
๐ Time and Auxiliary Space Complexity
Expected Time Complexity: O(mรn), where m is the number of rows and n is the number of columns. Each cell is visited at most twice - once during marking and once during DFS traversal.
Expected Auxiliary Space Complexity: O(mรn), as in the worst case the recursion stack can go as deep as the number of cells in the grid for DFS traversal.
๐งโ๐ป Code (C++)
class Solution {
public:
void fill(vector<vector<char>>& g) {
int m = g.size(), n = g[0].size();
for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) if (g[i][j] == 'O') g[i][j] = '-';
function<void(int, int)> dfs = [&](int i, int j) {
if (i < 0 || i >= m || j < 0 || j >= n || g[i][j] != '-') return;
g[i][j] = 'O';
dfs(i + 1, j); dfs(i - 1, j); dfs(i, j + 1); dfs(i, j - 1);
};
for (int i = 0; i < m; i++) { dfs(i, 0); dfs(i, n - 1); }
for (int j = 0; j < n; j++) { dfs(0, j); dfs(m - 1, j); }
for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) if (g[i][j] == '-') g[i][j] = 'X';
}
};โ Code (Java)
class Solution {
public void fill(char[][] g) {
int m = g.length, n = g[0].length;
for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) if (g[i][j] == 'O') g[i][j] = '-';
for (int i = 0; i < m; i++) { dfs(g, i, 0, m, n); dfs(g, i, n - 1, m, n); }
for (int j = 0; j < n; j++) { dfs(g, 0, j, m, n); dfs(g, m - 1, j, m, n); }
for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) if (g[i][j] == '-') g[i][j] = 'X';
}
void dfs(char[][] g, int i, int j, int m, int n) {
if (i < 0 || i >= m || j < 0 || j >= n || g[i][j] != '-') return;
g[i][j] = 'O';
dfs(g, i + 1, j, m, n); dfs(g, i - 1, j, m, n); dfs(g, i, j + 1, m, n); dfs(g, i, j - 1, m, n);
}
}๐ Code (Python)
class Solution:
def fill(self, g):
m, n = len(g), len(g[0])
for i in range(m):
for j in range(n):
if g[i][j] == 'O': g[i][j] = '-'
def dfs(i, j):
if i < 0 or i >= m or j < 0 or j >= n or g[i][j] != '-': return
g[i][j] = 'O'
dfs(i + 1, j); dfs(i - 1, j); dfs(i, j + 1); dfs(i, j - 1)
for i in range(m): dfs(i, 0); dfs(i, n - 1)
for j in range(n): dfs(0, j); dfs(m - 1, j)
for i in range(m):
for j in range(n):
if g[i][j] == '-': g[i][j] = 'X'๐ง Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: ๐ฌ Any Questions?. Let's make this learning journey more collaborative!
โญ If you find this helpful, please give this repository a star! โญ
๐Visitor Count
Last updated