In an ocean, there are islands marked by 1. Water is represented by 0.
Determine how many unique shapes (no rotation or mirror) among these islands.
Islands are connected 4-directionally.
Complete the function numDistinctIslands in the editor.
numDistinctIslands has the following parameter:
int[][] grid: a 2D array of integers representing the ocean
Returns
int: the number of unique island shapes
Examples
01 · Example 1
grid = [[1, 1, 1, 1, 0, 0], [1, 1, 0, 0, 0, 1], [0, 0, 1, 1, 0, 1], [1, 1, 0, 0, 0, 0], [0, 0, 1, 1, 1, 1], [1, 0, 1, 1, 0, 0]] return = 4
There are 4 unique island shapes:
- the 2 6-sized islands,
- the 2 2-sized islands,
- the 1 2-sized island,
- the 1 1-sized island.
More Google problems
- Consolidate On-Call RotationsOA · Seen Jun 2026
- Detonate Bombs with Chain ReactionsONSITE INTERVIEW · Seen May 2026
- Evaluate a Nested Math ExpressionONSITE INTERVIEW · Seen May 2026
- Tic-Tac-Toe Game StatusPHONE SCREEN · Seen May 2026
- Longest Dictionary TokenizationPHONE SCREEN · Seen May 2026
- Minimum Cars for Rental RequestsONSITE INTERVIEW · Seen Apr 2026
- Shortest Path with Mandatory WaypointONSITE INTERVIEW · Seen Apr 2026
- Count Divisible Coin SelectionsOA · Seen Dec 2025
public int numDistinctIslands(int[][] grid) {
// write your code here
}
grid[[1, 1, 1, 1, 0, 0], [1, 1, 0, 0, 0, 1], [0, 0, 1, 1, 0, 1], [1, 1, 0, 0, 0, 0], [0, 0, 1, 1, 1, 1], [1, 0, 1, 1, 0, 0]]
expected4
sign in to submit