Number of Islands
Updated
Problem statement
You are given a map as an array of strings grid. Each string is one row, and every row has the same length. The character '1' is land and '0' is water.
An island is a group of land cells joined up, down, left or right. Two land cells that touch only at a corner are not joined. Everything outside the map is water.
Return the number of islands.
Function
numIslands(grid: String[]) → intExamples
Example 1
grid = ["1110","1010","1110","0001"]return = 2The eight land cells in the top-left 3 by 3 area form a ring around one water cell. Each of them touches the next one up, down, left or right, so the ring is one island.
The land cell in the bottom-right corner touches the ring only at a corner, so it is a second island. The answer is 2.
Example 2
grid = ["101","010","101"]return = 5The five land cells touch each other only at corners. None of them is joined to another, so each one is its own island.
Example 3
grid = ["000","000"]return = 0There is no land, so there are no islands.
Constraints
1 <= grid.length <= 3001 <= grid[i].length <= 300- All rows have the same length.
- Every character is
'0'or'1'.
More Tripjack Onsite Interview questions
Similar questions at other companies
- Number of IslandsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Number of Islands with Diagonal ConnectivityGoogle · ONSITE INTERVIEWGoogle interview questions
- Island Count and Maximum AreaOracle · ONSITE INTERVIEWOracle interview questions
- Mouse and Cheese ReachabilityMeta · ONSITE INTERVIEWMeta interview questions
- Number of IslandsTekion · ONSITE INTERVIEWTekion interview questions
- Grid Pathfinding with Obstacles (DFS)Roblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Number of IslandsGoldman Sachs · OA · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
- Number of IslandsBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Number of IslandsCrowdstrike · ONSITE INTERVIEWCrowdstrike interview questions
- Max Area of IslandRoku · ONSITE INTERVIEWRoku interview questions