[Solved] Equal Row and Column Pairs LeetCode Contest Problem

Given a 0-indexed n x n integer matrix gridreturn 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).

Equal Row and Column Pairs LeetCode Contest

Example 1:

[Solved] Equal Row and Column Pairs LeetCode Contest Problem
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:

[Solved] Equal Row and Column Pairs LeetCode Contest Problem
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

Solution

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;
    }
};
class Solution {
    public int equalPairs(int[][] g) {
		int n = g.length;
        int res = 0;
		//convert the numbers in each row to a single string
        Map<String, Integer> sToFreq = new HashMap<>();
        for (int i = 0; i < n; i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < n; j++) { 
                sb.append(g[i][j]);
                sb.append("+");
            }
            String s = sb.toString();
            sToFreq.put(s, sToFreq.getOrDefault(s, 0) + 1);
        }
        
		//convert the numbers in each col to string and find the matches
        for (int j = 0; j < n; j++) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < n; i++) { 
                sb.append(g[i][j]);
                sb.append("+");
            }
            String s = sb.toString();
            if (sToFreq.containsKey(s)) {
                res += sToFreq.get(s);
            }
        }
        return res;
    }
}
    def equalPairs(self, grid: List[List[int]]) -> int:
        pairs = 0
        cnt = Counter(tuple(row) for row in grid)
        for tpl in zip(*grid):
            pairs += cnt[tpl]
        return pairs
    def equalPairs(self, A):
        count = Counter(zip(*A))
        return sum(count[tuple(r)] for r in A)

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

๐Ÿ“Œ Nodejs and Android ๐Ÿ˜Ž
๐Ÿ“Œ Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *