Count all pairs of rows and columns which are equal
Last Updated :
29 Nov, 2022
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}.
Input:
Example matrix
Output: 6
An approach using Hashing:
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++
#include <bits/stdc++.h>
using namespace std;
int equalPairs(vector<vector< int > >& grid)
{
unordered_map<string, int > unmap;
for ( int i = 0; i < grid.size(); i++) {
string s = "" ;
for ( int j = 0; j < grid[0].size(); j++) {
s += to_string(grid[i][j]);
s += "*" ;
}
unmap[s]++;
}
int result = 0;
for ( int j = 0; j < grid[0].size(); j++) {
string s = "" ;
for ( int i = 0; i < grid.size(); i++) {
s += to_string(grid[i][j]);
s += "*" ;
}
result += unmap[s];
}
return result;
}
int main()
{
vector<vector< int > > arr = {
{ 2, 4, 1, 1 },
{ 4, 5, 6, 6 },
{ 1, 6, 4, 4 },
{ 1, 6, 4, 4 },
};
cout << equalPairs(arr);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int equalPairs( int grid[][])
{
HashMap<String, Integer> unmap
= new HashMap<String, Integer>();
for ( int i = 0 ; i < grid.length; i++) {
String s = "" ;
for ( int j = 0 ; j < grid[ 0 ].length; j++) {
s += Integer.toString(grid[i][j]);
s += "*" ;
}
if (unmap.get(s) == null ) {
unmap.put(s, 1 );
}
else {
unmap.put(s, unmap.get(s) + 1 );
}
}
int result = 0 ;
for ( int j = 0 ; j < grid[ 0 ].length; j++) {
String s = "" ;
for ( int i = 0 ; i < grid.length; i++) {
s += Integer.toString(grid[i][j]);
s += "*" ;
}
result += unmap.get(s);
}
return result;
}
public static void main(String[] args)
{
int arr[][] = {
{ 2 , 4 , 1 , 1 },
{ 4 , 5 , 6 , 6 },
{ 1 , 6 , 4 , 4 },
{ 1 , 6 , 4 , 4 },
};
System.out.print(equalPairs(arr));
}
}
|
Python3
def equalPairs(grid) :
unmap = {};
for i in range ( len (grid)) :
s = "";
for j in range ( len (grid[ 0 ])) :
s + = str (grid[i][j]);
s + = "*" ;
if s in unmap :
unmap[s] + = 1 ;
else :
unmap[s] = 1 ;
result = 0 ;
for j in range ( len (grid[ 0 ])) :
s = "";
for i in range ( len (grid)) :
s + = str (grid[i][j]);
s + = "*" ;
result + = unmap[s];
return result;
if __name__ = = "__main__" :
arr = [
[ 2 , 4 , 1 , 1 ],
[ 4 , 5 , 6 , 6 ],
[ 1 , 6 , 4 , 4 ],
[ 1 , 6 , 4 , 4 ],
];
print (equalPairs(arr));
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int equalPairs( int [, ] grid, int N, int M)
{
Dictionary< string , int > mp
= new Dictionary< string , int >();
for ( int i = 0; i < N; i++) {
string s = "" ;
for ( int j = 0; j < M; j++) {
s += grid[i, j].ToString();
s += "*" ;
}
if (mp.ContainsKey(s)) {
int val = mp[s];
mp.Remove(s);
mp.Add(s, val + 1);
}
else
mp.Add(s, 1);
}
int result = 0;
for ( int j = 0; j < M; j++) {
string s = "" ;
for ( int i = 0; i < N; i++) {
s += grid[i, j].ToString();
s += "*" ;
}
result += mp[s];
}
return result;
}
static void Main()
{
int [, ] arr = {
{ 2, 4, 1, 1 },
{ 4, 5, 6, 6 },
{ 1, 6, 4, 4 },
{ 1, 6, 4, 4 },
};
Console.Write(equalPairs(arr, 4, 4));
}
}
|
Javascript
function equalPairs(grid)
{
let unmap = new Map();
for (let i = 0; i < grid.length; i++) {
let s = "" ;
for (let j = 0; j < grid[0].length; j++) {
s += (grid[i][j]).toString();
s += "*" ;
}
if (unmap.has(s)) {
unmap.set(s, unmap.get(s) + 1)
}
else {
unmap.set(s, 1)
}
}
let result = 0;
for (let j = 0; j < grid[0].length; j++) {
let s = "" ;
for (let i = 0; i < grid.length; i++) {
s += grid[i][j].toString();
s += "*" ;
}
result += unmap.get(s);
}
return result;
}
let arr = [
[ 2, 4, 1, 1 ],
[ 4, 5, 6, 6 ],
[ 1, 6, 4, 4 ],
[ 1, 6, 4, 4 ],
];
console.log(equalPairs(arr));
|
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...