forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_2352.java
36 lines (34 loc) · 1.23 KB
/
_2352.java
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
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class _2352 {
public static class Solution1 {
public int equalPairs(int[][] grid) {
Map<List<Integer>, Integer> rows = new HashMap<>();
for (int i = 0; i < grid.length; i++) {
List<Integer> row = new ArrayList<>();
for (int j = 0; j < grid[0].length; j++) {
row.add(grid[i][j]);
}
rows.put(row, rows.getOrDefault(row, 0) + 1);
}
Map<List<Integer>, Integer> columns = new HashMap<>();
for (int j = 0; j < grid[0].length; j++) {
List<Integer> column = new ArrayList<>();
for (int i = 0; i < grid.length; i++) {
column.add(grid[i][j]);
}
columns.put(column, columns.getOrDefault(column, 0) + 1);
}
int pairs = 0;
for (List<Integer> row : rows.keySet()) {
if (columns.containsKey(row)) {
pairs += rows.get(row) * columns.get(row);
}
}
return pairs;
}
}
}