Trains the technique from
LeetCode 694Number of Distinct IslandsThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A wetland survey arrives as grid, a rectangle of cells where 1 marks a cell full of reeds and 0 marks open water.
A patch is a largest possible collection of reed cells that can be reached from one another by repeatedly stepping to a cell directly above, below, to the left or to the right. Two reed cells that meet only at a corner belong to different patches.
Two patches count as the same shape when one can be slid horizontally and vertically, without any turning or flipping, so that its cells land exactly on the other patch's cells. Return how many different shapes the survey contains.
Example 1
There are four patches of three cells each. The patch rooted at row 0 column 0, the one at row 0 column 4 and the one at row 3 column 1 all slide onto one another. The patch in the bottom right corner covers row 3 column 5, row 4 column 4 and row 4 column 5, which no slide turns into any of the others, so two shapes occur.
Example 2
Corner contact does not join cells, so this survey holds three patches of one cell each. Every single cell slides onto every other, leaving one shape.
Example 3
Both patches hold four cells arranged as two horizontal pairs on adjacent rows. In the upper patch the lower pair sits one column to the right of the upper pair; in the lower patch it sits one column to the left, and sliding cannot fix that, so two shapes occur.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def num_distinct_islands(grid: list[list[int]]) -> int:public int numDistinctIslands(int[][] grid)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.