Number of Islands
Understand the problem
Given a 2D grid of characters '1' (land) and '0' (water). An island is a group of '1' cells adjacent in 4 directions (up, down, left, right). The grid is surrounded by water on all edges. Count the islands in the grid.
Examples:
ts
grid = [
["1","1","1","1","0"],
["1","1","0","1","0"],
["1","1","0","0","0"],
["0","0","0","0","0"]
]
output = 1 // all the connected land is 1 island
grid = [
["1","1","0","0","0"],
["1","1","0","0","0"],
["0","0","1","0","0"],
["0","0","0","1","1"]
]
output = 3 // 3 land regions that are not connected to each otherUnlock the optimal solution, the step-by-step trace and related questions
See the full step-by-step trace, Big-O and edge cases.