Leetcode Problem 694. Number of Distinct Islands

694. Number of Distinct Islands

Leetcode Solutions

Hash By Path Signature

  1. Initialize a set to store the unique hashes of islands.
  2. Iterate over each cell in the grid.
  3. If a cell is land (1) and not visited, start a DFS from that cell.
  4. During DFS, keep track of the path by appending directions to a string as you move to new cells.
  5. Also, append a special character (e.g., '0') when backtracking.
  6. After completing the DFS for an island, add the path string to the set.
  7. Continue until all cells are visited.
  8. Return the size of the set, which represents the number of distinct islands.
UML Thumbnail

Brute Force

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...