Counting Distinct Islands

Start Timer

0:00:00

Upvote
0
Downvote
Save question
View comments

Given a 2D matrix where 'X' represents land and 'O' represents ocean, write a function called count_islands that returns the number of distinct islands. An island is a group of horizontally or vertically connected land cells surrounded by ocean.

Note: Diagonal connections do not count as part of the same island.

Example:

Input:

grid = [
    ['X', 'O', 'O', 'X', 'X'],
    ['X', 'O', 'O', 'O', 'O'],
    ['O', 'O', 'X', 'O', 'X'],
    ['O', 'O', 'X', 'O', 'X'],
]

Output:

count_islands(grid) -> 4
.
.
.
.
.


Comments

Loading comments