Surrounded Regions Problem

Surrounded Regions Problem — ExecCode Hard DSA Practice

Solve the Surrounded Regions problem on ExecCode. Free online hard DSA practice in Arrays - Basics. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Given an m×n board containing 'X' and 'O', capture all regions that are surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. Any 'O' on the border (or connected to a border 'O') is not captured.

Examples

Input board = [["X", "X", "X", "X", "X"], ["X", "O", "O", "X", "O"], ["X", "O", "X", "X", "X"], ["X", "X", "O", "O", "X"], ["X", "X", "X", "X", "X"]]; Output "X,X,X,X,X,X,X,X,X,O,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X". Input board = [["X"]]; Output "X"

Constraints

m == board.length n == board[i].length 1 ≤ m, n ≤ 200 board[i][j] is 'X' or 'O'

Practice Surrounded Regions free on ExecCode. Browse DSA problems, topic map, and placement guides.