Problem

You are given two m x n binary matrices grid1 and grid2 containing only 0’s (representing water) and 1’s (representing land). An island is a group of 1’s connected 4-directionally (horizontal or vertical). Any cells outside of the grid are considered water cells.

An island in grid2 is considered a sub-island if there is an island in grid1 that contains all the cells that make up this island in grid2.

Return the number of islands in grid2 that are considered sub-islands.

Examples

Example 1:

$$ \begin{bmatrix} \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{blue} 0 & \colorbox{blue} 0 \\ \colorbox{blue} 0 & \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{orange} 1 \\ \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{blue} 0 \\ \colorbox{orange} 1 & \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{blue} 0 \\ \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{blue} 0 & \colorbox{orange} 1 & \colorbox{orange} 1 \end{bmatrix} \begin{bmatrix} \colorbox{red} 1 & \colorbox{red} 1 & \colorbox{red} 1 & \colorbox{blue} 0 & \colorbox{blue} 0 \\ \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{red} 1 & \colorbox{red} 1 & \colorbox{red} 1 \\ \colorbox{blue} 0 & \colorbox{orange} 1 & \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{blue} 0 \\ \colorbox{red} 1 & \colorbox{blue} 0 & \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{blue} 0 \\ \colorbox{blue} 0 & \colorbox{red} 1 & \colorbox{blue} 0 & \colorbox{orange} 1 & \colorbox{blue} 0 \end{bmatrix} $$

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
Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
The 1s colored red in grid2 are those considered to be part of a sub-island. There are three sub-islands.

Example 2:

$$ \begin{bmatrix} \colorbox{orange} 1 & \colorbox{blue} 0 & \colorbox{orange} 1 & \colorbox{blue} 0 & \colorbox{orange} 1 \\ \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{orange} 1 \\ \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{blue} 0 \\ \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{orange} 1 \\ \colorbox{orange} 1 & \colorbox{blue} 0 & \colorbox{orange} 1 & \colorbox{blue} 0 & \colorbox{orange} 1 \end{bmatrix} \begin{bmatrix} \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{blue} 0 \\ \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{orange} 1 & \colorbox{orange} 1 \\ \colorbox{blue} 0 & \colorbox{orange} 1 & \colorbox{blue} 0 & \colorbox{orange} 1 & \colorbox{blue} 0 \\ \colorbox{blue} 0 & \colorbox{orange} 1 & \colorbox{blue} 0 & \colorbox{orange} 1 & \colorbox{blue} 0 \\ \colorbox{red} 1 & \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{blue} 0 & \colorbox{red} 1 \end{bmatrix} $$

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,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1] ]
Output: 2 
Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
The 1s colored red in grid2 are those considered to be part of a sub-island. There are two sub-islands.

Solution

This problem is similar to Number of Islands.

Method 1 - DFS

Return true, if that is a sub-island. Also, when we visit some sub-island, make it visited by adding -1 in the adjacent pieces.

Here are the steps:

  1. DFS traversal for islands: Use DFS to identify and traverse islands in grid2.
  2. Check for Sub-island: While traversing an island in grid2, ensure every land cell (1) of this island corresponds to a land cell in grid1.
  3. Count Valid Sub-islands: Count islands in grid2 that pass the sub-island check.

Video Explanation

Here is the video explanation:

Code

Java
class Solution {
	private int m;
	private int n;
    public int countSubIslands(int[][] grid1, int[][] grid2) {
        this.m = grid2.length;
        this.n = grid2[0].length;

        int count = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid2[i][j] == 1) {
                    if (dfs(grid1, grid2, i, j) == true) {
                        count++;
                    }
                }
            }
        }
        return count;
    }

    private boolean dfs(int[][] grid1, int[][] grid2, int i, int j) {
        if (i < 0 || j < 0 || i >= m || j >= n || grid2[i][j] != 1) {
            return true;
        }
        if (grid1[i][j] != grid2[i][j]) {
            return false;
        }
        grid2[i][j] = -1;

        boolean left = dfs(grid1, grid2, i - 1, j);
        boolean right = dfs(grid1, grid2, i + 1, j);
        boolean up = dfs(grid1, grid2, i, j + 1);
        boolean down = dfs(grid1, grid2, i, j - 1);

        return left && right && up && down;
    }
}

Also, we don’t have to use -1, we can also use 0. When we use 0, that means island disappears, once we counted it :D.

Complexity

  • ⏰ Time complexity: O(m*n), where m is number of rows and n is number of columns.
    • Because we go through each cell only once, as when we start the dfs we sink the island to mark it as visited.
  • 🧺 Space complexity: O(m*n) assuming recursion stack