Skip to content
Problem 130 · LeetCode 130(opens in a new tab)

Surrounded Regions

MediumDepth-first search

You are given a board of m rows and n columns. Every cell holds "X" or "O". Cells are neighbours when they share a side (up, down, left or right), and a region is a group of O cells joined through neighbours.

A region is trapped when none of its cells lies on the outer edge of the board. Change every cell of every trapped region to X. Regions that touch the edge stay as they are. Modify board itself; the function returns nothing.

Example 1

Input:  [["X","O","X","X"],
         ["X","O","X","O"],
         ["X","X","O","X"],
         ["O","X","X","X"]]
Output: [["X","O","X","X"],
         ["X","O","X","O"],
         ["X","X","X","X"],
         ["O","X","X","X"]]
Why:    (2,2) is walled in; (0,1) is on the edge and saves (1,1)
2 more examples and the constraints

Example 2

Input:  [["O"]]
Output: [["O"]]
Why:    a single cell is on the edge

Example 3

Input:  [["O","X","O"],["X","O","X"],["O","X","O"]]
Output: [["O","X","O"],["X","X","X"],["O","X","O"]]
Why:    corners touch the middle only diagonally, so it is trapped

Constraints

  • 1 ≤ m, n ≤ 200
  • Every cell is "X" or "O"

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Search from every OO((m·n)²)
  2. Mark from the borderO(m·n)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for a 200 × 200 boardVerdict
Search from every OO((m·n)²)
Mention it, then move on
Mark from the borderO(m·n)
Best; each cell once

m = rows, n = columns.

Edge cases to test

  • One row or one column. Every cell is on the edge, so nothing is captured.
  • Diagonal neighbours. An O that touches a safe cell only at a corner is still trapped.
  • A long winding path. A region deep inside can be saved by a thin path that reaches the edge far away.
  • All O. Every region touches the edge, so the board does not change.
  • No O at all. Nothing to do; the board stays all X.

Common mistakes

  • Capturing cells while still searching, so a region that is saved later has already lost cells.
  • Counting diagonal cells as neighbours.
  • Using recursion on a 200 × 200 board and overflowing the call stack.

How to talk through it

  1. 1Clarify. Do diagonals count? Should I change the board in place? Can the board be a single row?
  2. 2Give the brute force. Search from each O to see if it escapes: O((m·n)²) because regions are walked again and again.
  3. 3Flip the question. Only cells connected to the edge survive, so search once, outward from the edge.
  4. 4Code the marking. Mark border O cells as S, spread with a stack, then one pass fixes every cell.
  5. 5Test out loud. Run the diagonal case and a one-row board before saying you are done.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?