Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Capture Surrounded Regions Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Capture Surrounded Regions

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

You are given a matrix board of size n*m, containing either 'X' or 'O' in each cell. 'X' represents cells you own, and 'O' represents those you don't. You can capture regions that are surrounded by 'X' in all directions by turning each of their cells from 'O' to 'X'. Note: Two cells are connected if they share a common edge. You have to return the board configuration after you have captured as many cells as you can.

Approach

The Approach to solve this problem is to use DFS on a grid to fill the valid connected components with the given value. But, here we have to use DFS intelligently. Observe that only those connected components which have a cell at any of the boundaries of the matrix are invalid because they cannot be surrounded completely. So rather than marking those cells which are valid, we can mark those cells which are invalid, by applying DFS on those cells with ‘O’ and which are on the boundary of the matrix and lie in the same connected component. After that, we can just iterate on the matrix and and change those cells which are unvisited with ‘X’. 

Analysis

  • Time Complexity: O(n * m)
  • Space Complexity: O(n * m) // due to visited array

Implementation

C++
void dfs(vector<vector<char>> &board, vector<vector<bool>> &visited, int i, int j) {
	if(i < 0 || j < 0 || i >= board.size() || j >= board[0].size()) {
		return;
	}
	if(board[i][j] == 'X' || visited[i][j]) {
		return;
	}
	visited[i][j] = true;
	dfs(board, vis, i + 1, j);
	dfs(board, vis, i - 1, j);
	dfs(board, vis, i, j + 1);
	dfs(board, vis, i, j - 1);
}
vector<vector<char>> getFinalBoard(vector<vector<char>> board) {
    int n = board.size();
	int m = board[0].size();
	vector<vector<bool>> visited(n, vector<bool> (m, false));
	for (int i = 0; i < n; i++) {
		if(!visited[i][0] && board[i][0] == 'O') {
			dfs(board, visited, i, 0);
		}
		if(!visited[i][m - 1] && board[i][m - 1] == 'O') {
			dfs(board, visited, i, m - 1);
		}
	}
	for (int i = 0; i < m; i++) {
		if(!visited[0][i] && board[0][i] == 'O') {
			dfs(board, visited, 0, i);
		}
		if(!visited[n - 1][i] && board[n - 1][i] == 'O') {
			dfs(board, visited, n - 1, i);
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if(!visited[i][j] && board[i][j] == 'O') {
				board[i][j] = 'X';
			}
		}
	}
	return board;
}
Java
class Solution {
	void dfs(char[][] board, boolean[][] visited, int i, int j) {
		if(i < 0 || j < 0 || i >= board.length || j >= board[0].length) {
			return;
		}
		if(board[i][j] == 'X' || visited[i][j]) {
			return;
		}
		visited[i][j] = true;
		dfs(board, vis, i + 1, j);
		dfs(board, vis, i - 1, j);
		dfs(board, vis, i, j + 1);
		dfs(board, vis, i, j - 1);
	}
	char[][] getFinalBoard(char[][] board) {
    	int n = board.length;
		int m = board[0].length;
		boolean[][] visited = new boolean[n + 1][m + 1];
		for (int i = 0; i < n; i++) {
			if(!visited[i][0] && board[i][0] == 'O') {
				dfs(board, visited, i, 0);
			}
			if(!visited[i][m - 1] && board[i][m - 1] == 'O') {
				dfs(board, visited, i, m - 1);
			}
		}
		for (int i = 0; i < m; i++) {
			if(!visited[0][i] && board[0][i] == 'O') {
				dfs(board, visited, 0, i);
			}
			if(!visited[n - 1][i] && board[n - 1][i] == 'O') {
				dfs(board, visited, n - 1, i);
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if(!visited[i][j] && board[i][j] == 'O') {
					board[i][j] = 'X';
				}
			}
		}
		return board;
	}
}
Related Content
Adjacency List to Adjacency Matrix
Adjacency Matrix to Adjacency List
BFS of a Graph
Clone a Graph
DFS of an Acyclic Graph
DFS of a Cyclic Graph
Edges to Adjacency List
Flood Fill Image
Knight's Journey On A Chessboard
M-Coloring Problem
Number of Islands
Stepping Numbers
Valid Path
Word Search Board
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet