993. Surrounded Regions — Capture Os

EasyGraphsGridDFSBFS

Given an m x n board of 'X' and 'O' characters, capture all regions of 'O' that are fully surrounded by 'X' by flipping them to 'X'. An 'O' region is safe (not captured) if any of its cells touches the border. Return the resulting board. The input is JSON {board}.

Input: JSON {board}.

Output: Nested array — the updated board.

Examples

Example 1
Input: {"board":[["X","X","X"],["X","O","X"],["X","X","X"]]}
Output: [["X","X","X"],["X","X","X"],["X","X","X"]]
Explanation: The enclosed O is captured.
Example 2
Input: {"board":[["O"]]}
Output: [["O"]]
Explanation: Border O stays.
Example 3
Input: {"board":[["X","O","X"],["O","O","O"],["X","O","X"]]}
Output: [["X","O","X"],["O","O","O"],["X","O","X"]]
Explanation: All Os touch the border.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →