Number of islands

Problem Given an m x n grid of '1' (land) and '0' (water), count the number of islands. An island is a maximal group of land cells connected 4-directionally (up/down/left/right).

Input / Output

  • Input: a 2D grid of '1'/'0' characters.
  • Output: the number of distinct islands (integer).

Constraints

  • 1 <= m, n <= 300. The grid may be all land or all water.
  • Connectivity is 4-directional only — diagonals do not connect.

Example

  • grid = [["1","1","0"],["1","0","0"],["0","0","1"]] → 2.
asked …
LeaderboardSalaryAccount