[LeetCode] Equal Row and Column Pairs

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).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
int equalPairs(vector<vector<int>>& A) {
unordered_map<string,int> mp;
int n = A.size(), m = A[0].size();
for(int i = 0; i < n; i++) {
string now = "";
for(int j = 0; j < m; j++) {
now += to_string(A[i][j]) + "#";
}
mp[now]++;
}
int res = 0;
for(int i = 0; i < m; i++) {
string now = "";
for(int j = 0; j < n; j++) {
now += to_string(A[j][i]) + "#";
}
res += mp[now];
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/07/24/PS/LeetCode/equal-row-and-column-pairs/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.