Island Adventure: Finding Sub-Islands!

You're an oceanographer studying two different maps of the same ocean region. You have two m x n binary grids representing the same area at different times:

grid1: The reference map (0 = water, 1 = land)
grid2: The current map (0 = water, 1 = land)

An island is a group of connected land cells (1's) that touch horizontally or vertically. Your mission is to find how many islands in grid2 are completely contained within islands from grid1.

What makes a sub-island?
An island in grid2 is a sub-island if every single cell of that island corresponds to land (1) in grid1. If even one cell of the island in grid2 is water (0) in grid1, then it's not a sub-island.

Goal: Return the total count of valid sub-islands in grid2.

Input & Output

example_1.py — Basic Sub-Island Detection
$ Input: grid1 = [[1,1,1,0,0],[0,1,1,1,1],[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1]] grid2 = [[1,1,1,0,0],[0,0,1,1,1],[0,1,0,0,0],[1,0,1,1,0],[0,1,0,1,0]]
Output: 3
💡 Note: Grid2 has several islands, but only 3 qualify as sub-islands: (1) Top-left island at (0,0)-(0,2) matches land in grid1, (2) Island at (1,3)-(1,4) corresponds to land in grid1, (3) Single cell island at (3,0) matches grid1. Other islands fail because they contain cells that are water in grid1.
example_2.py — All Valid Sub-Islands
$ Input: grid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]] grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,0,0,0,0],[0,1,1,1,0],[0,0,0,0,0]]
Output: 1
💡 Note: Grid2 has 2 islands: one large island in row 1 and a smaller island in row 3. The large island (1,0)-(1,4) perfectly matches land in grid1, making it a valid sub-island. The smaller island (3,1)-(3,3) also corresponds to land in grid1, making it valid too. Wait - let me recount: there's actually 1 connected island spanning positions that all correspond to land in grid1.
example_3.py — No Valid Sub-Islands
$ Input: grid1 = [[1,0,0,0,1],[0,1,1,1,0],[0,1,1,1,0],[0,1,1,1,0],[1,0,0,0,1]] grid2 = [[0,1,1,1,0],[1,0,0,0,1],[1,0,0,0,1],[1,0,0,0,1],[0,1,1,1,0]]
Output: 0
💡 Note: Every island in grid2 contains at least one cell that corresponds to water (0) in grid1. For example, the top island in grid2 at positions (0,1)-(0,3) overlaps with water in grid1, making it invalid. Similarly, all other islands fail the sub-island test.

Constraints

  • Grid dimensions: m == grid1.length == grid2.length
  • Grid dimensions: n == grid1[i].length == grid2[i].length
  • Size limits: 1 ≤ m, n ≤ 500
  • Cell values: grid1[i][j] and grid2[i][j] are either 0 or 1
  • Definition: Islands are 4-directionally connected (no diagonal connections)

Visualization

Tap to expand
Count Sub Islands - DFS Approach INPUT grid1 (Reference Map): grid2 (Current Map): Land (1) Water (0) Not Sub ALGORITHM STEPS 1 Find Islands in grid2 Use DFS to explore each connected land component 2 Check Each Cell For every land cell in grid2 verify grid1[i][j] == 1 3 Mark Invalid Islands If any cell has grid1=0, island is NOT a sub-island 4 Count Valid Sub-Islands Increment count only if ALL cells are covered DFS Traversal Pattern: S Explore all 4 directions recursively FINAL RESULT Sub-Island 1 (Top-left): OK Sub-Island 2 (Bottom-left): OK Sub-Island 3 (Bottom-right): OK Invalid (Not Sub-Islands): grid1=0 at these cells Output: 3 Key Insight: An island in grid2 is a sub-island ONLY if every single cell of that island corresponds to land (1) in grid1. Use DFS to traverse each island in grid2, checking grid1 at each step. If ANY cell fails the check, the entire island is invalid. Time: O(m*n) | Space: O(m*n) for recursion stack. TutorialsPoint - Count Sub Islands | DFS Approach
Asked in
Google 42 Amazon 38 Meta 35 Microsoft 28 Apple 22
48.2K Views
Medium-High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen