Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
PHP Program to Check if all rows of a matrix are circular rotations of each other
A rectangular array called a matrix is made up of rows and columns. And circular rotations entail rotating the array's elements so that after one rotation, the last member is in the first position and the other elements are shifted to the right. We are given an N*N matrix in this problem, and our objective is to determine whether all of the rows are circular rotations of one another. If they are, print "YES," otherwise print "NO."
In order to better understand the issue, let's look at some cases with explanations below.
Input 1
mat = [ [ 7, 8, 9],
[ 9, 7, 8],
[ 8, 9, 7] ]
Output 1
YES
Explanation
As consider the first row as a given array [ 7, 8, 9] now check for the remaining rows [9, 7, 8] and [8, 9, 7]:
Rotations for the second row [9, 7, 8] are: [8, 9, 7] and [7, 8, 9]
Second rotation is matched to the first row.
Rotations for the third row [8, 9, 7] are: [7, 8, 9] and [9, 7, 8]
First rotation is matched to the first row.
Therefore the answer is "YES" as all rows are permutations of each other.
Input 2
mat = [ [ 8, 9, 4],
[ 9, 8, 4] ]
Output 2
NO
Explanation
As consider the first row as a given array [8, 9, 4] now check for the remaining rows [9, 8, 4]:
Rotations for the second row [9, 8, 4] are: [4, 9, 8] and [8, 4, 9]
None of them are equal to the first row.
Therefore, the answer is "NO" as all rows are not permutations of each other.
Approach: String Concatenation Method
The concept behind this method is to store the first row as a string and then combine it with itself so that it may be used for substring search operations. Then, iterate through the matrix to examine the other rows. After converting each row to a string, check to see whether it is a substring of the string from the first row ?
Example
PHP program to check if all rows of a matrix are circular rotations of each other ?
<?php
// Create a Function to check every row are circular rotations to each other or not.
function circularRotations(&$mat, $n){
// Making a string that contains the first row's integers.
$strFirstRow = "";
// Traverse the matrix to store the first row as a string
for ($i = 0; $i < $n; $i++) {
// add the first row numbers to the variable strFirstRow
$strFirstRow = $strFirstRow . "-" . strval($mat[0][$i]);
}
// Combining the string strFirstRow with strFirstRow to enable substring search
$strFirstRow = $strFirstRow . $strFirstRow;
// Traverse the matrix from 1 row to check all remaining rows
for ($i = 1; $i < $n; $i++){
// Creating variable strCurrentRow and initialize it
$strCurrentRow = "";
// Traverse the current row
for ($j = 0; $j < $n; $j++) {
// Store current rows number to the variable strCurrentRow
$strCurrentRow = $strCurrentRow . "-" . strval($mat[$i][$j]);
}
// Verify whether the combining string strFirstRow contains the strCurrentRow string or not.
if (strpos($strFirstRow, $strCurrentRow) === false){
// If any row is not a rotation of first row, return false
return false;
}
}
// If all rows are circular rotations of first row, return true
return true;
}
$n = 4; // Given size of the matrix
// Given matrix
$mat = array(array(6, 7, 8, 9),
array(9, 6, 7, 8),
array(8, 9, 6, 7),
array(7, 8, 9, 6));
// call function to check all rows of matrix are circular rotations of each other or not
if (circularRotations($mat, $n)){
echo "YES, all rows of the matrix are circular rotations of each other";
} else{
echo "NO, all rows of the matrix are not circular rotations of each other";
}
?>
YES, all rows of the matrix are circular rotations of each other
How It Works
The algorithm works by
- Converting the first row to a string with delimiter
- Concatenating this string with itself to create all possible rotations
- For each subsequent row, converting it to a string and checking if it exists as a substring in the doubled first row string
- If any row is not found, return false; otherwise, return true
Time and Space Complexity
The time complexity of the above code is O(N^3), as we traverse the matrix and use the strval and strpos functions.
The space complexity of the above code is O(N), as we need to store rows of the matrix as strings.
Where N is the size of the row and column of the matrix.
Conclusion
In this tutorial, we have implemented a PHP program to check if all rows of a matrix are circular rotations of each other using string concatenation method. The approach converts rows into strings and uses substring search to verify rotations with O(N^3) time complexity.
