FastPrepCount Isolated Islands

Count Isolated Islands

Microsoft logoMicrosoft● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Given a rectangular binary grid represented by equal-length strings, count the islands of land cells marked '1' that do not touch the grid boundary.

Land cells connect horizontally or vertically. An isolated island is a maximal connected land component with no boundary cell.

Function

countIsolatedIslands(grid: String[]) → int

Examples

Example 1

grid = ["00000","01100","01010","00010","00000"]return = 2

The upper-left three-cell component and the right two-cell component are separate, and neither touches the boundary.

Example 2

grid = ["111","101","111"]return = 0

The only land component touches the boundary, so it is not isolated.

Constraints

  • 1 <= grid.length <= 300
  • 1 <= grid[i].length <= 300, and all rows have equal length
  • Every character is '0' or '1'

More Microsoft problems

See Microsoft hiring insights
public int countIsolatedIslands(String[] grid) {
  // Write your code here.
}
grid["00000","01100","01010","00010","00000"]
expected2
Checking account…