0%

Leetcode694-numberOfDistinctIslands

Description

Given a non-empty 2D array grid of 0’s and 1’s, 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.

Count the number of distinct islands. 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.

Example

Example 1:

1
2
3
4
11000
11000
00011
00011

Given the above grid map, return 1.

Example 2:

1
2
3
4
11011
10000
00001
11011

Given the above grid map, return 3.

Notice that:

1
2
11
1

and
1
2
 1
11

are considered different island shapes, because we do not consider reflection / rotation.

Note: The length of each dimension in the given grid does not exceed 50.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public int numDistinctIslands(int[][] grid) {
if (grid == null || grid.length == 0 || grid[0].length == 0) return 0;
HashSet<String> set = new HashSet<>();
for (int i = 0; i < grid.length; i++){
for (int j = 0; j < grid[0].length; j++){
if (grid[i][j] != 0){
StringBuilder cur= new StringBuilder();
helper(grid, i, j, cur, "o");
set.add(cur.toString());
}
}
}
return set.size();
}

private int[][] direction = new int[][]{{1, 0},{-1, 0},{0, 1},{0, -1}};
private String[] symbols = new String[]{"d", "u", "r", "l"};
private void helper(int[][] grid, int x, int y, StringBuilder cur, String dir){
grid[x][y] = 0;
cur.append(dir);
for (int i = 0; i < direction.length; i++){
int nx = x + direction[i][0];
int ny = y + direction[i][1];
if (nx >= 0 && nx < grid.length && ny >= 0 && ny < grid[0].length && grid[nx][ny] == 1){
helper(grid, nx, ny, cur, symbols[i]);
}
}
cur.append("b");
}
}