Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

146. Surrounded Regions

MediumDay 43 of 60Graphs

You are given an m x n board of "X" and "O". A region of "O" cells (connected up, down, left, right) is surrounded if none of its cells is on the border of the board. Capture every surrounded region by turning all its "O" cells into "X". Modify the board in place; the function returns nothing.

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"]]
The middle region is captured. The "O" on the bottom edge is on the border, so it stays.
Example 2
Input: board = [["X"]]
Output: [["X"]]
Constraints
  • 1 <= m, n <= 200
  • board[i][j] is "X" or "O"
Hints
Frequently asked at: Google, Amazon, Uber
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min