304 North Cardinal St.
Dorchester Center, MA 02124

# Equal Row and Column Pairs LeetCode Solution

## Problem – Equal Row and Column Pairs LeetCode Solution

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

Example 1:

``````Input: grid = [[3,2,1],[1,7,6],[2,7,7]]
Output: 1
Explanation: There is 1 equal row and column pair:
- (Row 2, Column 1): [2,7,7]
``````

Example 2:

``````Input: grid = [[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]]
Output: 3
Explanation: There are 3 equal row and column pairs:
- (Row 0, Column 0): [3,1,2,2]
- (Row 2, Column 2): [2,4,2,2]
- (Row 3, Column 2): [2,4,2,2]``````

Constraints:

• `n == grid.length == grid[i].length`
• `1 <= n <= 200`
• `1 <= grid[i][j] <= 105`

### Equal Row and Column Pairs LeetCode Solution in C++

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

for (int i = 0; i < grid[0].size(); i++)
{
vector<int> v;
// extracting column in a vector.
for (int j = 0; j < grid.size(); j++)
v.push_back(grid[j][i]);
// Add the number of times that column appeared as a row.
ans += mp[v];
}
// Return the number of count
return ans;
}
};
``````

### Equal Row and Column Pairs LeetCode Solution in Java

``````public int equalPairs(int[][] grid) {
int res = 0, n = grid.length;
HashMap<String, Integer> x = new HashMap<>();
HashMap<String, Integer> y = new HashMap<>();

for(int i=0; i<n; i++){
StringBuilder sb1 = new StringBuilder(), sb2 = new StringBuilder();
for(int j=0; j<n; j++){
sb1.append(grid[i][j]);
sb2.append(grid[j][i]);
sb1.append(','); sb2.append(',');
}
String curr1 = sb1.toString(), curr2 = sb2.toString();
x.put(curr1, x.getOrDefault(curr1, 0)+1);
y.put(curr2, y.getOrDefault(curr2, 0)+1);
}

for(String str : x.keySet())
if(y.containsKey(str))
res += x.get(str)*y.get(str);
return res;
}
``````

### Equal Row and Column Pairs LeetCode Solution in Python

``````    def equalPairs(self, A):
count = Counter(zip(*A))
return sum(count[tuple(r)] for r in A)
``````
##### Equal Row and Column Pairs LeetCode Solution Review:

In our experience, we suggest you solve this Equal Row and Column Pairs LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Equal Row and Column Pairs LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Equal Row and Column Pairs LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions