Count the Islands

Hard40–60 pts depending on language · +25 for learning a language that's new to you

A map is given as a list of rows, where # is land and . is water. Land squares that touch up, down, left or right (not diagonally) belong to the same island. Return how many islands there are.

countIslands(["##...", "##...", "..#..", "...##"]) returns 3.

Hint: when you find land you haven't visited yet, count a new island, then visit everything connected to it (flood fill) so you don't count it again.

The map has 1 to 60 rows, every row is the same length (1 to 60 characters), and it only contains # and ..

Examples

Input: countIslands(["##...", "##...", "..#..", "...##"])

Output: 3

Input: countIslands(["#.#", "...", "#.#"])

Output: 4

Your solution is checked against 2 example tests and 9 hidden tests (edge cases and bigger inputs).

Solved by 1 member

  1. 1Zach🐧🐧C# · Oct 15
Full leaderboard →

Your Solution

Languages you don't learn in class are worth more: C++ 1.25×, JavaScript, TypeScript, C, Rust and Go 1.5×. Your first solve in a language unlocks its "Learned" achievement: +25 for learning it.

Loading editor...

IntelliSense: connecting…

Run Code checks the example tests. Submit runs every test, including hidden ones.