Ctrl K

Distinct Island Shapes

Medium
You are given a map chart of zeros and ones: 1 is land and 0 is water. An island is a group of land cells joined through their sides (horizontally or vertically; touching diagonally does not count). Everything outside the map is water. Two islands have the same shape if one can be moved onto the other by sliding it, without rotating or mirroring. Return how many different shapes appear among all the islands on the map.
Example 1
chart=[[1, 1, 0, 1, 1], [0, 0, 0, 0, 0], [1, 0, 0, 1, 1], [1, 0, 0, 0, 0]] → 2

The two horizontal two-cell strips share one shape, the vertical strip at the bottom left has another. Two shapes in total.

Example 2
chart=[[1, 0], [1, 1], [0, 0], [0, 1], [1, 1]] → 2

Both islands are three-cell corners, but the second mirrors the first. Reflections and rotations are not allowed, so the shapes differ.

Example 3
chart=[[0, 0], [0, 0]] → 0
Constraints
  • 1 ≤ chart.length, chart[i].length ≤ 50
  • All rows have the same length
  • chart[i][j] is 0 or 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsIsland Shapes Up to Symmetry HardMerge Contact Cards MediumChains of Synonyms MediumA Board Cut by Slashes MediumRearrange Through Links Medium
loading Python…
Input
chart=[[1, 1, 0, 1, 1], [0, 0, 0, 0, 0], [1, 0, 0, 1, 1], [1, 0, 0, 0, 0]]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 67 tests on the server, hidden ones included.