| Topic | Difficulty | Companies |
|---|---|---|
| Graph Algorithms | HARD | Google |
Given a 2-D matrix board where every element is either ‘O’ or ‘X’, write a program to replace ‘O’ with ‘X’ if surrounded by ‘X’.
Problem Note
O’ or a set of ‘O’ is considered to be by surrounded by ‘X’ if there are ‘X’ at locations just below, just above just left and just right of it.'O' on the border of the board are not flipped to 'X'.'O' that is not on the border and it is not connected to an 'O' on the border will be flipped to 'X'.Example 1
Input: board[][] = [
['X', 'O', 'X', 'X', 'X', 'X'],
['X', 'O', 'X', 'X', 'O', 'X'],
['X', 'X', 'X', 'O', 'O', 'X'],
['O', 'X', 'X', 'X', 'X', 'X'],
['X', 'X', 'X', 'O', 'X', 'O'],
['O', 'O', 'X', 'O', 'O', 'O'],
]
Output: board[][] = [
['X', 'O', 'X', 'X', 'X', 'X'],
['X', 'O', 'X', 'X', 'X', 'X'],
['X', 'X', 'X', 'X', 'X', 'X'],
['O', 'X', 'X', 'X', 'X', 'X'],
['X', 'X', 'X', 'O', 'X', 'O'],
['O', 'O', 'X', 'O', 'O', 'O'],
]
Example 2
Input: board[][] = [
['X', 'X', 'X', 'X']
['X', 'O', 'X', 'X']
['X', 'O', 'O', 'X']
['X', 'O', 'X', 'X']
['X', 'X', 'O', 'O']
]
Output: board[][] = [
['X', 'X', 'X', 'X']
['X', 'X', 'X', 'X']
['X', 'X', 'X', 'X']
['X', 'X', 'X', 'X']
['X', 'X', 'O', 'O']
]