FunctionRacerProblemsRace

Count the Lakes

EasyGraphsPython, JavaScript, TypeScript, Java, C++

chart is a rectangular map given as a list of strings of equal length. Each character is '~' for water or '#' for land.

Two water cells are joined when they touch along a side or at a corner, so each cell has up to eight neighbours. A lake is a maximal group of water cells in which every cell can be reached from every other through joined water cells. Water on the edge of the map is still part of a lake. Return the number of lakes; return 0 when there is no water.

Example 1

chart ["~~#","#~#","##~"]
returns 1

The three water cells in the top left touch along sides, and the bottom right cell touches the middle one at a corner, so all four form one lake.

Example 2

chart ["###","###"]
returns 0

There is no water.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Graphs problems