Surrounded Regions
Graph
medium
Score: 40
Given an m x n
matrix board containing ‘X’ and 'O’, capture all regions that are 4-directionally surrounded by 'X’.
A region is captured by flipping all 'O’s into 'X’s in that surrounded region.
Input Format
First Parameter - matrix board
of string of size m x n
Output Format
Return the matrix of string.
Example 1:
Input:
4 4
X X X X
X O O X
X X O X
X O X X
Output:
X X X X
X X X X
X X X X
X O X X
Explanation: 4 4 represents the size of the board.
Surrounded regions should not be on the border, which means that any 'O' on the border of the board are not flipped to 'X'. Any 'O' that is not on the border and it is not connected to an 'O' on the border will be flipped to 'X'. Two cells are connected if they are adjacent cells connected horizontally or vertically.
Constraints:
m
==board.length
n
==board[i].length
- 1 <=
m, n
<= 200 board[i][j]
is ‘X’ or 'O’.- Expected Time Complexity: O(n*m)
- Expected Auxiliary Space: O(n*m)