On January 25, as we celebrate new beginnings and connections, your task is to count the number of distinct islands in a grid. An island is made up of adjacent land cells (represented by '1'
) connected horizontally or vertically. Water is represented by '0'
.
Given a 2D grid of characters, write a function that returns the number of islands. Two cells belong to the same island if they are both '1'
and are connected horizontally or vertically.
Input:
[
["1", "1", "0", "0", "0"],
["1", "1", "0", "0", "0"],
["0", "0", "1", "0", "0"],
["0", "0", "0", "1", "1"]
]
Output: 3
Below is some starter code in multiple languages to help you begin your solution.