Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Number of Islands

XPChallenge Points: 20
levelLevel: Medium

You are given a 2D grid of characters '1' (land) and '0' (water).
Return how many separate islands (connected land areas) exist.
Land cells that touch up, down, left, or right belong to the same island.

Example 1:

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

Output: 3

Explanation:

Explanation • Island 1: top-left block • Island 2: single cell in row 3 • Island 3: bottom-right corner

Example 2:

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

Output: 5

Explanation:

Every '1' stands alone, so there are 5 islands.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result