Equal Row and Column Pairs LeetCode solution in C++

1
class Solution {
2
public:
3
int equalPairs(vector<vector<int>> &grid) {
4
// Number to store the count of equal pairs.
5
int ans = 0;
6
map<vector<int>, int> mp;
7
// Storing each row int he map
8
for (int i = 0; i < grid.size(); i++) mp[grid[i]]++;
9

10
for (int i = 0; i < grid[0].size(); i++) {
11
vector<int> v;
12
// extracting column in a vector.
13
for (int j = 0; j < grid.size(); j++) v.push_back(grid[j][i]);
14
// Add the number of times that column appeared as a row.
15
ans += mp[v];
16
}
17
// Return the number of count
18
return ans;
19
}
20
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0