Sobes.tech
Junior — Senior

Counting the number of islands in a two-dimensional grid

livecode

Task condition

Given a 2D array of characters '1' (land) and '0' (water) of size m × n, representing a map. Return the number of islands. An island is a group of adjacent cells (vertically or horizontally) with the character '1', completely surrounded by cells with '0'. It is assumed that the outer boundaries of the map are also surrounded by water.

Example 1: Input: map = [ ["1","1","1","1","0"], ["1","1","0","1","0"], ["1","1","0","0","0"], ["0","0","0","0","0"] ] Output: 1

Example 2: Input: map = [ ["1","1","0","0","0"], ["1","1","0","0","0"], ["0","0","1","0","0"], ["0","0","0","1","1"] ] Output: 3