Number of Distinct Islands - Problem
You are given an m x n binary matrix grid. 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.
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.
Return the number of distinct islands.
Input & Output
Example 1 — Basic Islands
$
Input:
grid = [[1,1,0,1,1],[1,1,0,1,1],[0,0,0,0,0],[1,1,0,1,1],[1,1,0,1,1]]
›
Output:
1
💡 Note:
The grid has 4 islands total, all with the same 2x2 square shape. When normalized, they all have the same relative coordinates: (0,0), (0,1), (1,0), (1,1). Therefore, there is only 1 distinct island shape.
Example 2 — L-shaped Islands
$
Input:
grid = [[1,1,0],[1,0,0],[0,0,1]]
›
Output:
2
💡 Note:
First island is L-shaped at positions (0,0),(0,1),(1,0). Second island is a single cell at (2,2). These are 2 different shapes when normalized.
Example 3 — Single Cell
$
Input:
grid = [[1,0,1],[0,0,0],[1,0,1]]
›
Output:
1
💡 Note:
All four islands are single cells at (0,0), (0,2), (2,0), (2,2). They all have the same shape: just one cell at relative position (0,0).
Constraints
- 1 ≤ m, n ≤ 50
- grid[i][j] is either 0 or 1
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code