-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_2352.java
More file actions
29 lines (28 loc) · 1.06 KB
/
Copy pathproblem_2352.java
File metadata and controls
29 lines (28 loc) · 1.06 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
/*
2352. Equal Row and Column Pairs
Given a 0-indexed n x n integer matrix grid, return the number of pairs (ri, cj) such that row ri and column cj are equal.
A row and column pair is considered equal if they contain the same elements in the same order (i.e., an equal array). */
class Solution {
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 key = sb.toString();
rowMap.put(key, rowMap.getOrDefault(key, 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 key = sb.toString();
count += rowMap.getOrDefault(key, 0);
}
return count;
}
}