← Practice · BFS / DFS

Number of Islands Medium

Given a 2D grid of the strings "1" (land) and "0" (water), return the number of islands. An island is a group of land cells connected horizontally or vertically.

Examples

  • [["1","1","0"],["0","0","0"],["0","0","1"]] -> 2
  • [["0"]] -> 0

Define a function named numIslands in JavaScript.

00:00▶ Visualize

JavaScript · Tab indents · Ctrl/⌘+Enter runs the tests