Equal Row and Column Pairs LeetCode solution in Java

1
class Solution {
2
public int equalPairs(int[][] grid) {
3
HashMap<String, Integer> map = new HashMap<>();
4
int row = grid.length;
5
int col = grid.length;
6
for (int i = 0; i < row; i++) {
7
String res = "";
8
for (int j = 0; j < col; j++) {
9
res += "-" + grid[i][j];
10
}
11
map.put(res, map.getOrDefault(res, 0) + 1);
12
}
13
int cnt = 0;
14
for (int j = 0; j < col; j++) {
15
String res = "";
16
for (int i = 0; i < row; i++) {
17
res += "-" + grid[i][j];
18
}
19
cnt += map.getOrDefault(res, 0);
20
}
21
return cnt;
22
}
23
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0