Skip to content
Decoder

Surrounded Regions

Medium

Problem

A board holds "X" and "O" cells. A group of "O" cells connected up, down, left or right is captured when none of its cells lies on the edge of the board: flip every cell of a captured group to "X". Groups that touch the border survive. Change board in place; the function returns nothing.

Example 1

X X X X        X X X X

Example 2

Input
X O O X
Output
X X X X

Example 3

X X O X        X X X X

Example 4

X O X X        X O X X   (the bottom O touches the border, so it stays)

Constraints

  • 1 <= board.length, board[i].length <= 200
  • board[i][j] is "X" or "O"

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.