Problem · Matrix
Number of Islands
Learn this problemProblem statement
Given an array of equal-length strings grid, where '1' represents land and '0' represents water, return the number of islands.
An island is a maximal group of land cells connected horizontally or vertically. Cells outside the matrix are water.
Function
countIslands(grid: String[]) → intExamples
Example 1
grid = ["11110","11010","11000","00000"]return = 1All land cells belong to one orthogonally connected component.
Example 2
grid = ["11000","11000","00100","00011"]return = 3The upper-left block, the center cell, and the lower-right pair form three separate islands.
Example 3
grid = ["000","000"]return = 0The matrix contains no land cells.
Constraints
1 <= grid.length <= 3001 <= grid[i].length <= 300- Every row has the same length and contains only
'0'and'1'.
More Tekion problems
- Koko Eating BananasONSITE INTERVIEW · Seen Jul 2026
- Maximum Sum of a Unique-Element SubarrayONSITE INTERVIEW · Seen Jul 2026
- Longest Balanced Substring After One SwapOA · Seen Jul 2026
- Minimum Size Subarray Sum With NegativesONSITE INTERVIEW · Seen Jul 2026
- Set Matrix ZeroesONSITE INTERVIEW · Seen Jul 2026