#130

Surrounded Regions

specialist · 965 · lc medium +32 · 44.8% accepted · 9,818 likes · top 27%

play →

Description

You are given an m x n matrix board of 'X' and 'O' characters. Capture all surrounded regions:

- Connect: Cells are adjacent if they share a horizontal or vertical edge.

- Region: A region is a group of connected 'O' cells.

- Surround: A region is surrounded when none of its 'O' cells touch the board's border.

Replace every 'O' in a surrounded region with 'X' in-place. Do not return anything.

Code

1
2
3
4
5
6