bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 711. Number of Distinct Islands II

711. Number of Distinct Islands II

Leetcode Solutions

Canonical Hash for Distinct Island Shapes

  1. Iterate over each cell in the grid.
  2. If the cell is part of an island (value is 1) and has not been seen before, perform a Depth-First Search (DFS) to explore the entire island.
  3. During the DFS, record the cells that make up the island.
  4. For each island, generate all possible rotations and reflections.
  5. Normalize these shapes by translating them so that the bottom-left-most coordinate is at (0, 0).
  6. Sort the coordinates of each transformed shape and choose the smallest one as the canonical representation.
  7. Add the canonical representation of the island to a set to ensure distinct shapes are counted.
  8. Return the size of the set, which represents the number of distinct islands.
UML Thumbnail

Distinct Islands with String Serialization

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...