Surrounded Regions

BFS & DFS Problems DSA practice problem on Onlearn.

Difficulty: hard.

Topics: Replace all ‘O’ with ‘X’ in a matrix surrounded by ‘X’, Matrices, Two-dimensional Array Traversal, Depth-First Search, Breadth-First Search, Graph Traversal, Time Complexity, Space Complexity, Recursion, Arrays, In-place Algorithm, edge cases, complexity analysis, graph algorithms, visited tracking, dfs, matrix traversal, bfs, Graph Traversal Utilities.

Given a matrix mat of size N x M where every element is either ‘O’ or ‘X’. Replace all ‘O’ with ‘X’ that is surrounded by ‘X’. An ‘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. Input Specification: The input will consist of a 2D array of characters, mat, representing the matrix. Output Specification: Return the modified matrix where all surrounded 'O's are replaced by 'X's. Sample Input: Sample Output: