Number of Islands

Medium · Graphs · Array · DFS · BFS · Union Find · Matrix

Problem

Given a 2-D grid of "1" (land) and "0" (water), count the islands.

An island is a group of land cells connected horizontally or vertically (not diagonally). The grid is surrounded by water on all sides.

Examples

Input: grid = [["1","1","0"],
         ["1","0","0"],
         ["0","0","1"]]
Output: 2

The three connected cells on the left, plus the lone cell bottom-right.

Constraints

Approaches

Related problems

Start free on Cariara · Coding problems · System design studio

Starting Python
No results yet.

          
↑↓ move ↵ open esc close

Labs

Loading labs…

Run with your own input

Starting Python

Loading Python…