Count all pairs of rows and columns which are equal
Last Updated : 23 Jul, 2025
Given a matrix mat[][] of size N * N (1 ? N ? 1000), the task is to find the number of pairs of rows and columns (Rowi, Colj), such that row Rowi and column Colj are equal.
Note: A row and column pair are considered equal if they contain the same elements in the same order (i.e. an equal array).
Example:
Input:
Matrix
Output: 3 Explanation: The row-column pairs are {1, 1}, {3, 2}, {3, 3}.
Iterate on the matrix and keep all the elements of each row into a string and store these strings into map with their frequency. Iterate over the map and keep all the elements of each column into a string and then check if column string has any occurrence into map. If there exist any occurrence into map then keep adding their count into result.
Follow the steps below to implement the above idea:
Initialize a map for mapping all columns with their frequency of occurrence.
Iterate over every row
Initialize an empty string s = "".
Keep appending all the elements into the string s.
Increment the frequency count of string s in the map
Initialize a result variable for storing the number of pairs such that the row and column are equal
Iterate over every column
Initialize an empty string s = "".
Keep appending all the elements into the string s.
Add into the result of the frequency count of s in the map.
Return the result.
Below is the implementation of the above approach.
C++
// C++ code to implement the approach#include<bits/stdc++.h>usingnamespacestd;// Function to count the row-column pairsintequalPairs(vector<vector<int>>&grid){// Initialize a map for mapping all// column with their frequency of// occurrence.unordered_map<string,int>unmap;// Iterate over all rowsfor(inti=0;i<grid.size();i++){// Initialize an empty string s = ""strings="";// Keep appending all the element// into string sfor(intj=0;j<grid[0].size();j++){s+=to_string(grid[i][j]);s+="*";}// Increment the frequency count of// string s in mapunmap[s]++;}// Initialize a result variable for// storing the number of pair such that// row and column are equalintresult=0;// Iterate over all the columnsfor(intj=0;j<grid[0].size();j++){// Initialize an empty string s = ""strings="";// Keep appending all the element// into string sfor(inti=0;i<grid.size();i++){s+=to_string(grid[i][j]);s+="*";}// Add into result of frequency// count of s in map.result+=unmap[s];}// Return the resultreturnresult;}// Driver codeintmain(){vector<vector<int>>arr={{2,4,1,1},{4,5,6,6},{1,6,4,4},{1,6,4,4},};// Function Callcout<<equalPairs(arr);return0;}
Java
// Java code to implement the approachimportjava.io.*;importjava.util.*;classGFG{// Function to count the row-column pairspublicstaticintequalPairs(intgrid[][]){// Initialize a map for mapping all// column with their frequency of// occurrence.HashMap<String,Integer>unmap=newHashMap<String,Integer>();// Iterate over all rowsfor(inti=0;i<grid.length;i++){// Initialize an empty string s = ""Strings="";// Keep appending all the element// into string sfor(intj=0;j<grid[0].length;j++){s+=Integer.toString(grid[i][j]);s+="*";}// Increment the frequency count of// string s in mapif(unmap.get(s)==null){unmap.put(s,1);}else{unmap.put(s,unmap.get(s)+1);}}// Initialize a result variable for// storing the number of pair such that// row and column are equalintresult=0;// Iterate over all the columnsfor(intj=0;j<grid[0].length;j++){// Initialize an empty string s = ""Strings="";// Keep appending all the element// into string sfor(inti=0;i<grid.length;i++){s+=Integer.toString(grid[i][j]);s+="*";}// The condition check is required// for cases like the below// { 3,2,1},// { 1,7,6},// { 2,7,7},if(unmap.containsKey(s)){result+=unmap.get(s);}}// Return the resultreturnresult;}// Driver Codepublicstaticvoidmain(String[]args){intarr[][]={{2,4,1,1},{4,5,6,6},{1,6,4,4},{1,6,4,4},};// Function CallSystem.out.print(equalPairs(arr));}}// This code is contributed by Rohit Pradhan
Python
# Python3 code to implement the approach# Function to count the row-column pairsdefequalPairs(grid):# Initialize a map for mapping all# column with their frequency of# occurrence.unmap={};# Iterate over all rowsforiinrange(len(grid)):# Initialize an empty string s = ""s="";# Keep appending all the element# into string sforjinrange(len(grid[0])):s+=str(grid[i][j]);s+="*";ifsinunmap:# Increment the frequency count of# string s in mapunmap[s]+=1;else:unmap[s]=1;# Initialize a result variable for# storing the number of pair such that# row and column are equalresult=0;# Iterate over all the columnsforjinrange(len(grid[0])):# Initialize an empty string s = ""s="";# Keep appending all the element# into string sforiinrange(len(grid)):s+=str(grid[i][j]);s+="*";# Add into result of frequency# count of s in map.result+=unmap[s];# Return the resultreturnresult;# Driver codeif__name__=="__main__":arr=[[2,4,1,1],[4,5,6,6],[1,6,4,4],[1,6,4,4],];# Function Callprint(equalPairs(arr));# This code is contributed by AnkThon
C#
// C# code to implement the approachusingSystem;usingSystem.Collections.Generic;classGFG{// Function to count the row-column pairsstaticintequalPairs(int[,]grid,intN,intM){// Initialize a map for mapping all// column with their frequency of// occurrence.Dictionary<string,int>mp=newDictionary<string,int>();// Iterate over all rowsfor(inti=0;i<N;i++){// Initialize an empty string s = ""strings="";// Keep appending all the element// into string sfor(intj=0;j<M;j++){s+=grid[i,j].ToString();s+="*";}// Increment the frequency count of// string s in mapif(mp.ContainsKey(s)){intval=mp[s];mp.Remove(s);mp.Add(s,val+1);}elsemp.Add(s,1);}// Initialize a result variable for// storing the number of pair such that// row and column are equalintresult=0;// Iterate over all the columnsfor(intj=0;j<M;j++){// Initialize an empty string s = ""strings="";// Keep appending all the element// into string sfor(inti=0;i<N;i++){s+=grid[i,j].ToString();s+="*";}// Add into result of frequency// count of s in map.result+=mp[s];}// Return the resultreturnresult;}staticvoidMain(){int[,]arr={{2,4,1,1},{4,5,6,6},{1,6,4,4},{1,6,4,4},};// Function CallConsole.Write(equalPairs(arr,4,4));}}// This code is contributed by garg28harsh.
JavaScript
// JavaScript code for the above approach// Function to count the row-column pairsfunctionequalPairs(grid){// Initialize a map for mapping all// column with their frequency of// occurrence.letunmap=newMap();// Iterate over all rowsfor(leti=0;i<grid.length;i++){// Initialize an empty string s = ""lets="";// Keep appending all the element// into string sfor(letj=0;j<grid[0].length;j++){s+=(grid[i][j]).toString();s+="*";}// Increment the frequency count of// string s in mapif(unmap.has(s)){unmap.set(s,unmap.get(s)+1)}else{unmap.set(s,1)}}// Initialize a result variable for// storing the number of pair such that// row and column are equalletresult=0;// Iterate over all the columnsfor(letj=0;j<grid[0].length;j++){// Initialize an empty string s = ""lets="";// Keep appending all the element// into string sfor(leti=0;i<grid.length;i++){s+=grid[i][j].toString();s+="*";}// Add into result of frequency// count of s in map.result+=unmap.get(s);}// Return the resultreturnresult;}// Driver codeletarr=[[2,4,1,1],[4,5,6,6],[1,6,4,4],[1,6,4,4],];// Function Callconsole.log(equalPairs(arr));// This code is contributed by Potta Lokesh