Number of Islands Problem

Number of Islands Problem — ExecCode Medium DSA Practice

Solve the Number of Islands problem on ExecCode. Free online medium DSA practice in Arrays - Basics. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Given an m×n 2D binary grid which represents a map of '1's (land) and '0's (water), return the number of islands. An island is surrounded by water and formed by connecting adjacent lands horizontally or vertically.

Examples

Input grid = [["1", "1", "0", "0", "0", "1"], ["1", "0", "0", "1", "0", "1"], ["0", "0", "0", "1", "0", "0"], ["0", "1", "1", "0", "0", "1"], ["0", "1", "0", "0", "0", "1"]]; Output 5. Input grid = [["1", "1", "0"], ["0", "1", "0"], ["1", "0", "1"]]; Output 3. Input grid = [["0", "0"], ["0", "0"]]; Output 0

Constraints

m == grid.length n == grid[i].length 1 ≤ m, n ≤ 300 grid[i][j] is '0' or '1'

Practice Number of Islands free on ExecCode. Browse DSA problems, topic map, and placement guides.