Graphs Grids as graphs
439. Number of Distinct Islands
You are given a map as a grid of integers, grid, with m rows and n columns. A cell that holds 1 is
land, and a cell that holds 0 is water.
An island is a group of land cells joined by steps up, down, left or right. Two land cells that touch only at a corner are not joined. Everything outside the grid is water.
Two islands have the same shape when one can slide onto the other, without turning or flipping, so
that its land cells cover the other's land cells exactly. Return the number of different shapes among the
islands in grid.
Example 1
Input: grid = [[1, 1, 0, 0, 1, 1],
[1, 0, 0, 0, 1, 0],
[0, 0, 1, 0, 0, 0],
[0, 1, 1, 0, 1, 1],
[0, 0, 0, 0, 1, 0]]
2Explanation: There are four islands of three cells. The two at the top and the one at the bottom right have the same shape, since each can slide onto the others. The island in the middle is that shape turned around, and turning is not allowed, so it is a second shape.
Example 2
Input: grid = [[1, 0, 1], [0, 1, 0], [1, 0, 1]]
1Explanation: The five land cells touch only at corners, so each one is an island of a single cell. They all have the same shape.
Example 3
Input: grid = [[1, 1, 0, 1, 1], [1, 0, 0, 0, 1]]
2Explanation: Each island is the mirror image of the other. Flipping is not allowed, so no slide puts one onto the other.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
m == grid.length,n == grid[i].length1 ≤ m, n ≤ 300grid[i][j]is 0 or 1.
Clarifying questions
What you would ask an interviewer before writing any code.