Number of Distinct Islands

MEDIUM

Given a non-empty 2D array grid of 0’s and 1’s, an island is a group of 1‘s (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.

Count the number of distinct islands. An island is considered to be the same as another if and only if one island can be translated (and not rotated or reflected) to equal the other.

Example 1:

11000
11000
00011
00011

Given the above grid map, return 1.

Example 2:

11011
10000
00001
11011

Given the above grid map, return 3.

Notice that:

11
1

and

 1
11

are considered different island shapes, because we do not consider reflection / rotation.

Note: The length of each dimension in the given grid does not exceed 50.

We can use direction string the calculate the uniqueness of each island.

class Solution {
    public int numDistinctIslands(int[][] grid) {
        
        if(grid == null || grid.length == 0 || grid[0].length == 0){
            return 0;
        }
        int m = grid.length;
        int n = grid[0].length;
        Set<String> distinctIsland = new HashSet<>();
        for(int i = 0 ; i < m ; i++){
            for(int j = 0 ; j < n ; j++){
                if(grid[i][j] == 1){
                    StringBuilder encodeIsland = new StringBuilder();
                    DFS(grid, m, n, i, j, encodeIsland, "o"); // origin
                    String island = encodeIsland.toString();
                    if(!distinctIsland.contains(island)){
                        distinctIsland.add(island);
                    }
                }
            }
        }
        return distinctIsland.size();
    }
    
    private void DFS(int[][] grid, int m, int n, int i, int j,
                     StringBuilder encodeIsland, String direction) {
        if (i < 0 || i == m || j < 0 || j == n || grid[i][j] == 0) {
            return;
        }
        grid[i][j] = 0;
        encodeIsland.append("1").append(direction);
        DFS(grid, m, n, i + 1, j, encodeIsland, "b");  // bottom
        DFS(grid, m, n, i - 1, j, encodeIsland, "t");  // top
        DFS(grid, m, n, i, j + 1, encodeIsland, "r");  // right
        DFS(grid, m, n, i, j - 1, encodeIsland, "l");  // left
        encodeIsland.append("e"); // end
    }
}

Complexity Analysis

  • Time Complexity: O(RC), where R is the number of rows in the given grid, and C is the number of columns. We visit every square once.
  • Space Complexity: O(RC), the space used by seen to keep track of visited squares, and shapes.

Categories: DFS

Tagged as:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s