Skip to content

Number of Islands

MediumGraphs
Decode it first. Clues, constraints, pattern and plan before any code.

You get a map as a grid of strings: "1" is land and "0" is water. Land cells that touch up, down, left or right belong to the same island (diagonals do not count). Return how many islands there are.

[["1","1","0","0"],
 ["1","0","0","1"],
 ["0","0","1","1"]]   →  2
 
[["1","0"],
 ["0","1"]]           →  2   (diagonal cells are separate)

Constraints

  • 0 <= grid.length <= 300 and every row has the same length, at most 300
  • grid[i][j] is "0" or "1"
  • You may change the grid while you work.