Discover Unique Island Shapes! 🏝️

You're an explorer surveying a vast ocean represented by an m x n binary grid. In this grid, 1 represents land and 0 represents water. Islands are formed by groups of connected land cells that touch each other horizontally or vertically (4-directional connectivity).

Here's the twist: you need to identify how many distinct island shapes exist in this ocean. Two islands are considered the same shape if one can be translated (moved without rotation or reflection) to perfectly match the other.

Your Mission: Count the number of unique island shapes in the grid.

Example:
Grid: [[1,1,0],[0,1,1],[0,0,0]]
This contains one L-shaped island, so the answer is 1.

Input & Output

example_1.py β€” Basic L-shaped island
$ Input: grid = [[1,1,0,0,0],[1,0,0,0,0],[0,0,0,1,1],[0,0,0,1,1]]
β€Ί Output: 1
πŸ’‘ Note: The grid contains two identical L-shaped islands that can be translated to match each other exactly. Since they have the same shape, there is only 1 distinct island shape.
example_2.py β€” Different shaped islands
$ Input: grid = [[1,1,0,1,1],[1,0,0,0,0],[0,0,0,0,1],[1,1,0,1,1]]
β€Ί Output: 3
πŸ’‘ Note: This grid contains three different island shapes: an L-shape, a single cell, and a 2x1 rectangle. Each has a unique structure, so there are 3 distinct islands.
example_3.py β€” Single large island
$ Input: grid = [[1,1,1],[1,1,1]]
β€Ί Output: 1
πŸ’‘ Note: The entire grid forms one connected island in a 2x3 rectangular shape. Since there's only one island, the answer is 1.

Visualization

Tap to expand
πŸ—ΊοΈ Island Shape Discovery ProcessOcean GridStep 1: Scan GridDFS PathSRDS-R-D-B-BStep 2: Encode ShapeHash LookupO(1)LookupStep 3: Check CatalogResult3 DistinctIslands FoundStep 4: Count Unique🎯 Key Insight: Path EncodingBy recording the DFS traversal path (directions + backtracking), each island gets a unique signature.This allows O(1) duplicate detection using a hash set, achieving optimal O(mΓ—n) time complexity.
Understanding the Visualization
1
Discover Islands
Scan the grid to find uncharted islands using DFS exploration
2
Encode Shape
Create a unique signature by recording the path taken during exploration
3
Check Catalog
Compare against previously discovered shapes using hash set lookup
4
Update Collection
Add new unique shapes to our catalog of distinct island types
Key Takeaway
🎯 Key Insight: By encoding each island's shape as a unique DFS path signature, we can identify duplicates in constant time, achieving optimal performance with a single grid traversal.

Time & Space Complexity

Time Complexity
⏱️
O(m*n)

Each cell is visited exactly once during DFS traversal

n
2n
βœ“ Linear Growth
Space Complexity
O(m*n)

Hash set stores unique island signatures, visited array, and DFS recursion stack

n
2n
⚑ Linearithmic Space

Constraints

  • 1 ≀ m, n ≀ 50
  • grid[i][j] is either 0 or 1
  • Islands are connected 4-directionally (horizontal and vertical only)
  • All four edges of the grid are surrounded by water
Asked in
Google 47 Facebook 35 Amazon 28 Microsoft 22 Apple 18
42.4K Views
Medium-High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen