PHP Program to Check if all rows of a matrix are circular rotations of each other Last Updated : 23 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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, 1Output: Yes ,All rows are rotated permutation of each other.Input: mat[3][3] = 1, 2, 3 3, 2, 1 1, 3, 2Output: No, Explanation : As 3, 2, 1 is not a rotated or circular permutation of 1, 2, 3The idea is based on the below article. Program to check if strings are rotations of each other or notApproach: 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 the above steps. PHP <?php // PHP program to check if all rows of a // matrix are rotations of each other $MAX = 1000; // Returns true if all rows of mat[0..n-1][0..n-1] // are rotations of each other. function isPermutedMatrix( &$mat, $n) { // Creating a string that contains // elements of first row. $str_cat = ""; for ($i = 0 ; $i < $n ; $i++) $str_cat = $str_cat . "-" . strval($mat[0][$i]); // Concatenating the string with itself // so that substring search operations // can be performed on this $str_cat = $str_cat . $str_cat; // Start traversing remaining rows for ($i = 1; $i < $n; $i++) { // Store the matrix into vector // in the form of strings $curr_str = ""; for ($j = 0 ; $j < $n ; $j++) $curr_str = $curr_str . "-" . strval($mat[$i][$j]); // Check if the current string is present // in the concatenated string or not if (strpos($str_cat, $curr_str)) return true; } return false; } // Driver Code $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"; // This code is contributed by ita_c ?> OutputYes Complexity Analysis: Time complexity : O(n3) Auxiliary Space : O(n)Please refer complete article on Check if all rows of a matrix are circular rotations of each other for more details! Comment More infoAdvertise with us Next Article PHP Program to Check if all rows of a matrix are circular rotations of each other K kartik Follow Improve Article Tags : PHP rotation Similar Reads Javascript Program to Check if all rows of a matrix are circular rotations of each other 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, 1Output: YesAll rows are rotated permutationof each other.Input: mat[3][3] = 1, 2, 3 3, 2, 1 1, 3, 2Output: NoExplanation : As 3, 2, 1 is not 2 min read Check if all rows of a matrix are circular rotations of each other 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 8 min read PHP Program to check a string is a rotation of another string Given the two strings we have to check if one string is a rotation of another string. Examples: Input : $string1 = "WayToCrack", $string2 = "CrackWayTo"; Output : Yes Input : $string1 = "WillPower" $string2 = "lliW"; Output : No. The above problem can be easily solved in other languages by concatena 3 min read Check if Strings Are Rotations of Each Other Given two string s1 and s2 of same length, the task is to check whether s2 is a rotation of s1.Examples: Input: s1 = "abcd", s2 = "cdab"Output: trueExplanation: After 2 right rotations, s1 will become equal to s2.Input: s1 = "aab", s2 = "aba"Output: trueExplanation: After 1 left rotation, s1 will be 15+ min read Check whether Matrix T is a result of one or more 90° rotations of Matrix mat Given two 2D matrices mat[][] and T[][] of size MÃN and PÃQ respectively. The task is to check whether the matrix T[][] is a result of one or more 90° rotations of the matrix mat[][]. Examples: Input: mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, T[][] ={{7, 4, 1}, {8, 5, 2}, {9, 6, 3}}Output: YesExp 13 min read Like