Largest Island

EasyGraphs≈ 20 min

A map is given as a list of equal-length strings, one per row: # is land and . is water. Land cells that touch up, down, left or right belong to the same island; cells that only touch diagonally do not.

The size of an island is the number of land cells in it. Return the size of the largest island, or 0 if the map has no land.

Examples

text
["##...",
 "#..##",
 "...##",
 "#...."]     →  4

["....."]     →  0

["#.#",
 "###",
 "..#"]       →  6

["#.#.#"]     →  1

In the first example the islands have 3, 4 and 1 cells. In the third, all six land cells are joined.

Constraints

  • Up to 100 rows and 100 columns; the map may be empty.
  • Aim for O(n) in the number of cells: visit each cell a constant number of times.
Sound off. Turn on for key clicks and a chime when the tests pass.Inline suggestions off. Recommended while you are learning. Turn on to complete words as you type.
Console
›Run to see what your code prints

Run checks the 4 visible cases here, in your browser. Submit runs all 9 on the judge, 5 of them hidden, and measures how your solution scales.