The translation-only constraint is what makes this interesting.
Use DFS/BFS to identify each island, then compute a canonical representation of its shape by normalizing the coordinates relative to the top-left cell. Store these representations in a set to count distinct shapes.
Pro tip: Emphasize that translation invariance is achieved by subtracting the minimum row and column from each cell's coordinates, and mention that using a set of tuples or strings ensures efficient duplicate detection.
Iterate through each cell in the grid. When encountering an unvisited '1', start a DFS/BFS to explore the entire island.
During traversal, record the relative coordinates of each cell in the island with respect to the starting cell (or any reference point).
Translate the island so that its top-leftmost cell (minimum row and column) becomes the origin (0,0). This makes the representation invariant to translation.
Convert the normalized set of coordinates into a hashable form (e.g., a sorted tuple of coordinates or a string) and add it to a set.
After processing all islands, the size of the set gives the number of distinct island shapes.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.