-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEqualRowColumnPairs.java
More file actions
45 lines (34 loc) · 1.26 KB
/
Copy pathEqualRowColumnPairs.java
File metadata and controls
45 lines (34 loc) · 1.26 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package LeetcodePractice;
import java.util.HashMap;
import java.util.Map;
public class EqualRowColumnPairs {
public int equalPairs(int[][] grid) {
int n = grid.length;
Map<String, Integer> rowMap = new HashMap<>();
for (int i = 0; i < n; i++) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < n; j++) {
sb.append(grid[i][j]).append(",");
}
String rowStr = sb.toString();
rowMap.put(rowStr, rowMap.getOrDefault(rowStr, 0) + 1);
}
int count = 0;
for (int j = 0; j < n; j++) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append(grid[i][j]).append(",");
}
String colStr = sb.toString();
count += rowMap.getOrDefault(colStr, 0);
}
return count;
}
public static void main(String[] args) {
EqualRowColumnPairs solution = new EqualRowColumnPairs();
int[][] grid1 = {{3, 2, 1}, {1, 7, 6}, {2, 7, 7}};
System.out.println(solution.equalPairs(grid1));
int[][] grid2 = {{3,1,2,2},{1,4,4,5},{2,4,2,2},{2,4,2,2}};
System.out.println(solution.equalPairs(grid2));
}
}