Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Surrounded Regions

XPChallenge Points: 20
levelLevel: Medium

You are given a 2-D grid board with letters 'X' and 'O'.


• Cells connect up, down, left, and right.
• A region is a group of connected 'O' cells.
• If an 'O' region is fully surrounded by 'X' (and no 'O' from that region touches the outer edge of the grid),
change all 'O' in that region to 'X'.


Do the changes in the same board (no need to return a new grid).

Example 1:

Input: board = [ ["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:

The center 'O' cells are completely surrounded and become 'X'. The single 'O' in the bottom row touches the border, so it stays.

Example 2:

Input: board = [ ["O","O","X"], ["O","X","O"], ["X","O","X"] ]

Output: [ ["O","O","X"], ["O","X","O"], ["X","O","X"] ]

Explanation:

No region of 'O' is fully surrounded (all are connected to the border).

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result