Dsa
Graphs Dfs
Easy
Count Distinct Regions
Given a 2D grid where each cell is either 'X' or 'O', where 'X' represents land and 'O' represents water, your task is to count how many distinct regions of land ('X') are present, considering only 4-directional connectivity (up, down, left, right).
Input format:
- The first line contains two integers, m and n, representing the number of rows and columns in the grid.
- The next m lines each contain a string of length n, consisting of 'X's and 'O's.
Output format:
- Print a single integer representing the number of distinct regions of land.
Example Input:
4 5
XOOOX
XOOOX
OOXOO
XXOOO
Example Output:
3
Key concepts
graphsdfscounting
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now