You are given an m x n binary matrix grid. An island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.
An island is considered to be the same as another if and only if one island can be translated (and not rotated or reflected) to equal the other.
Return the number of distinct islands.
Example 1:
Input: grid = [[1,1,0,0,0],[1,1,0,0,0],[0,0,0,1,1],[0,0,0,1,1]]
Output: 1Example 2:
Input: grid = [[1,1,0,1,1],[1,0,0,0,0],[0,0,0,0,1],[1,1,0,1,1]]
Output: 3Constraints:
m == grid.lengthn == grid[i].length1 <= m, n <= 50grid[i][j] is either 0 or 1.class Solution:
def numDistinctIslands(self, grid: List[List[int]]) -> int:
def current_island_is_unique():
for other_island in unique_islands:
if len(other_island) != len(current_island):
continue
for cell_1, cell_2 in zip(current_island, other_island):
if cell_1 != cell_2:
break
else:
return False
return True
# Do a DFS to find all cells in the current island.
def dfs(row, col):
if row < 0 or col < 0 or row >= len(grid) or col >= len(grid[0]):
return
if (row, col) in seen or not grid[row][col]:
return
seen.add((row, col))
current_island.append((row - row_origin, col - col_origin))
dfs(row + 1, col)
dfs(row - 1, col)
dfs(row, col + 1)
dfs(row, col - 1)
# Repeatedly start DFS's as long as there are islands remaining.
seen = set()
unique_islands = []
for row in range(len(grid)):
for col in range(len(grid[0])):
current_island = []
row_origin = row
col_origin = col
dfs(row, col)
if not current_island or not current_island_is_unique():
continue
unique_islands.append(current_island)
print(unique_islands)
return len(unique_islands)Where is the number of rows, and is the number of columns
class Solution:
def numDistinctIslands(self, grid: List[List[int]]) -> int:
# Do a DFS to find all cells in the current island.
def dfs(row, col):
if row < 0 or col < 0 or row >= len(grid) or col >= len(grid[0]):
return
if (row, col) in seen or not grid[row][col]:
return
seen.add((row, col))
current_island.add((row - row_origin, col - col_origin))
dfs(row + 1, col)
dfs(row - 1, col)
dfs(row, col + 1)
dfs(row, col - 1)
# Repeatedly start DFS's as long as there are islands remaining.
seen = set()
unique_islands = set()
for row in range(len(grid)):
for col in range(len(grid[0])):
current_island = set()
row_origin = row
col_origin = col
dfs(row, col)
if current_island:
unique_islands.add(frozenset(current_island))
return len(unique_islands)Where is the number of rows, and is the number of columns
class Solution:
def numDistinctIslands(self, grid: List[List[int]]) -> int:
# Do a DFS to find all cells in the current island.
def dfs(row, col, direction):
if row < 0 or col < 0 or row >= len(grid) or col >= len(grid[0]):
return
if (row, col) in seen or not grid[row][col]:
return
seen.add((row, col))
path_signature.append(direction)
dfs(row + 1, col, "D")
dfs(row - 1, col, "U")
dfs(row, col + 1, "R")
dfs(row, col - 1, "L")
path_signature.append("0")
# Repeatedly start DFS's as long as there are islands remaining.
seen = set()
unique_islands = set()
for row in range(len(grid)):
for col in range(len(grid[0])):
path_signature = []
dfs(row, col, "0")
if path_signature:
unique_islands.add(tuple(path_signature))
return len(unique_islands)Where is the number of rows, and is the number of columns