Number of Islands — Python Coding Problem
Difficulty: medium | Category: graph
Problem Description
Given an `m x n` grid of `'1'` (land) and `'0'` (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent (horizontally or vertically) land cells. Hint: Use DFS or BFS. For each unvisited `'1'`, start a search and mark all connected land cells as visited. Increment a counter for each search started.
Examples
Example 1
Input: grid = [["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]]
Output: 1
Example 2
Input: grid = [["1","1","0","0","0"],["1","1","0","0","0"],["0","0","1","0","0"],["0","0","0","1","1"]]
Output: 3