Dsa
Union Find
Easy
Islands in a 2D Grid
You are given a 2D grid of '1's (land) and '0's (water). Your task is to find the number of distinct islands in the grid. An island is formed by adjacent '1's connected horizontally or vertically.
Input Format:
- The first line contains two integers R and C (1 ≤ R, C ≤ 1000), representing the number of rows and columns in the grid.
- The next R lines contain C characters (either '1' or '0').
Output Format:
- Print the number of distinct islands.
Example Input:
3 3
1 0 0
1 1 0
0 0 1
Example Output:
2
Key concepts
union_findgrid
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