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 ..
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).
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...
Run Code checks the example tests. Submit runs every test, including hidden ones.