LeetCode 694: Number of Distinct Islands Solution

Master LeetCode problem 694 (Number of Distinct Islands), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

Problem Explanation

Explanation:

To solve this problem, we can use a depth-first search (DFS) algorithm to explore each island in the grid and record the relative positions of all the cells in the island. We can represent each island as a string formed by the directions of the movements required to traverse all its cells.

  1. We start by iterating through each cell in the grid.
  2. When we encounter a cell that is part of an island (marked as '1'), we initiate a DFS traversal to explore the island.
  3. During the traversal, we keep track of the movements required to reach each cell relative to the starting cell of the island.
  4. After completing the traversal, we store the movements as a string representing the shape of the island.
  5. We repeat this process for all islands in the grid and store the unique island shapes in a set.
  6. Finally, we return the size of the set which corresponds to the number of distinct islands in the grid.

Time Complexity: O(mn) where m is the number of rows and n is the number of columns in the grid. Space Complexity: O(mn) for the recursion stack and the set to store unique island shapes.

:

Solution Code

class Solution {
    public int numDistinctIslands(int[][] grid) {
        Set<String> shapes = new HashSet<>();
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == 1) {
                    StringBuilder shape = new StringBuilder();
                    dfs(grid, i, j, i, j, shape);
                    shapes.add(shape.toString());
                }
            }
        }
        return shapes.size();
    }
    
    private void dfs(int[][] grid, int startI, int startJ, int i, int j, StringBuilder shape) {
        if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == 0) {
            return;
        }
        grid[i][j] = 0;
        shape.append(i - startI).append(j - startJ);
        dfs(grid, startI, startJ, i + 1, j, shape);
        dfs(grid, startI, startJ, i - 1, j, shape);
        dfs(grid, startI, startJ, i, j + 1, shape);
        dfs(grid, startI, startJ, i, j - 1, shape);
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 694 (Number of Distinct Islands)?

This page provides optimized solutions for LeetCode problem 694 (Number of Distinct Islands) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 694 (Number of Distinct Islands)?

The time complexity for LeetCode 694 (Number of Distinct Islands) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 694 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 694 in Java, C++, or Python.

Back to LeetCode Solutions