Number of Islands
medium · tree/graph
You are given a rectangular grid of characters, where '1' means land and '0' means water. An island is a group of '1's connected horizontally or vertically (not diagonally). Count how many islands there are and return that number.
Input: `grid` (a list of lists of '1' and '0' characters).
Output: the number of islands.
Example: grid = [["1", "1", "0"], ["0", "1", "0"], ["0", "0", "0"]] -> 1.
Starter code
def num_islands(grid: list[list[str]]) -> int:
"""Count the islands of '1's connected horizontally or vertically."""
pass
Enable JavaScript to edit this code and run the hidden tests in your browser. Each test has a five-second execution limit. Your drafts and solved progress stay in this browser.
Return to the Python compiler