Check if all rows of a matrix are circular rotations of each other
Last Updated :
12 Dec, 2022
Given a matrix of n*n size, the task is to find whether all rows are circular rotations of each other or not.
Examples:
Input: mat[][] = 1, 2, 3
3, 1, 2
2, 3, 1
Output: Yes
All rows are rotated permutation
of each other.
Input: mat[3][3] = 1, 2, 3
3, 2, 1
1, 3, 2
Output: No
Explanation : As 3, 2, 1 is not a rotated or
circular permutation of 1, 2, 3
The idea is based on below article.
A Program to check if strings are rotations of each other or not
Steps :
- Create a string of first row elements and concatenate the string with itself so that string search operations can be efficiently performed. Let this string be str_cat.
- Traverse all remaining rows. For every row being traversed, create a string str_curr of current row elements. If str_curr is not a substring of str_cat, return false.
- Return true.
Below is the implementation of above steps.
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000;
bool isPermutedMatrix( int mat[MAX][MAX], int n)
{
string str_cat = "" ;
for ( int i = 0 ; i < n ; i++)
str_cat = str_cat + "-" + to_string(mat[0][i]);
str_cat = str_cat + str_cat;
for ( int i=1; i<n; i++)
{
string curr_str = "" ;
for ( int j = 0 ; j < n ; j++)
curr_str = curr_str + "-" + to_string(mat[i][j]);
if (str_cat.find(curr_str) == string::npos)
return false ;
}
return true ;
}
int main()
{
int n = 4 ;
int mat[MAX][MAX] = {{1, 2, 3, 4},
{4, 1, 2, 3},
{3, 4, 1, 2},
{2, 3, 4, 1}
};
isPermutedMatrix(mat, n)? cout << "Yes" :
cout << "No" ;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int MAX = 1000 ;
static boolean isPermutedMatrix( int mat[][], int n)
{
String str_cat = "" ;
for ( int i = 0 ; i < n; i++)
{
str_cat = str_cat + "-" + String.valueOf(mat[ 0 ][i]);
}
str_cat = str_cat + str_cat;
for ( int i = 1 ; i < n; i++)
{
String curr_str = "" ;
for ( int j = 0 ; j < n; j++)
{
curr_str = curr_str + "-" + String.valueOf(mat[i][j]);
}
if (str_cat.contentEquals(curr_str))
{
return false ;
}
}
return true ;
}
public static void main(String[] args)
{
int n = 4 ;
int mat[][] = {{ 1 , 2 , 3 , 4 },
{ 4 , 1 , 2 , 3 },
{ 3 , 4 , 1 , 2 },
{ 2 , 3 , 4 , 1 }
};
if (isPermutedMatrix(mat, n))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
MAX = 1000
def isPermutedMatrix(mat, n) :
str_cat = ""
for i in range (n) :
str_cat = str_cat + "-" + str (mat[ 0 ][i])
str_cat = str_cat + str_cat
for i in range ( 1 , n) :
curr_str = ""
for j in range (n) :
curr_str = curr_str + "-" + str (mat[i][j])
if (str_cat.find(curr_str)) :
return True
return False
if __name__ = = "__main__" :
n = 4
mat = [[ 1 , 2 , 3 , 4 ],
[ 4 , 1 , 2 , 3 ],
[ 3 , 4 , 1 , 2 ],
[ 2 , 3 , 4 , 1 ]]
if (isPermutedMatrix(mat, n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool isPermutedMatrix( int [,]mat, int n)
{
string str_cat = "" ;
for ( int i = 0; i < n; i++)
{
str_cat = str_cat + "-" + mat[0,i].ToString();
}
str_cat = str_cat + str_cat;
for ( int i = 1; i < n; i++)
{
string curr_str = "" ;
for ( int j = 0; j < n; j++)
{
curr_str = curr_str + "-" + mat[i,j].ToString();
}
if (str_cat.Equals(curr_str))
{
return false ;
}
}
return true ;
}
static void Main()
{
int n = 4;
int [,]mat = {{1, 2, 3, 4},
{4, 1, 2, 3},
{3, 4, 1, 2},
{2, 3, 4, 1}
};
if (isPermutedMatrix(mat, n))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
PHP
<?php
$MAX = 1000;
function isPermutedMatrix( & $mat , $n )
{
$str_cat = "" ;
for ( $i = 0 ; $i < $n ; $i ++)
$str_cat = $str_cat . "-" .
strval ( $mat [0][ $i ]);
$str_cat = $str_cat . $str_cat ;
for ( $i = 1; $i < $n ; $i ++)
{
$curr_str = "" ;
for ( $j = 0 ; $j < $n ; $j ++)
$curr_str = $curr_str . "-" .
strval ( $mat [ $i ][ $j ]);
if ( strpos ( $str_cat , $curr_str ))
return true;
}
return false;
}
$n = 4;
$mat = array ( array (1, 2, 3, 4),
array (4, 1, 2, 3),
array (3, 4, 1, 2),
array (2, 3, 4, 1));
if (isPermutedMatrix( $mat , $n ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function isPermutedMatrix(mat, n)
{
let str_cat = "" ;
for (let i = 0; i < n; i++)
{
str_cat = str_cat + "-" +
(mat[0][i]).toString();
}
str_cat = str_cat + str_cat;
for (let i = 1; i < n; i++)
{
let curr_str = "" ;
for (let j = 0; j < n; j++)
{
curr_str = curr_str + "-" +
(mat[i][j]).toString();
}
if (str_cat.includes(curr_str))
{
return true ;
}
}
return false ;
}
let n = 4;
let mat = [ [ 1, 2, 3, 4 ],
[ 4, 1, 2, 3 ],
[ 3, 4, 1, 2 ],
[ 2, 3, 4, 1 ] ];
if (isPermutedMatrix(mat, n))
document.write( "Yes" )
else
document.write( "No" )
</script>
|
Time complexity: O(n3)
Auxiliary Space: O(n), since n extra space has been taken.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...