Javascript Program to Check if all rows of a matrix are circular rotations of each other Last Updated : 23 Sep, 2024 Comments Improve Suggest changes 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: 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 a rotated or circular permutation of 1, 2, 3The idea is based on the below article. A Program to check if strings are rotations of each other or notSteps : 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. JavaScript // Javascript program to check if all rows of a matrix // are rotations of each other // 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. let str_cat = ""; for (let i = 0; i < n; i++) { str_cat = str_cat + "-" + (mat[0][i]).toString(); } // 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 (let i = 1; i < n; i++) { // Store the matrix into vector in the form // of strings let curr_str = ""; for (let j = 0; j < n; j++) { curr_str = curr_str + "-" + (mat[i][j]).toString(); } // Check if the current string is present in // the concatenated string or not if (str_cat.includes(curr_str)) { return true; } } return false; } // Drivers code 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)) console.log("Yes") else console.log("No") OutputYes Complexity Analysis:Time Complexity: O(n^3) 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 Javascript Program to Check if all rows of a matrix are circular rotations of each other K kartik Follow Improve Article Tags : Strings Matrix JavaScript Web Technologies DSA rotation +2 More Practice Tags : MatrixStrings Similar Reads 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 Javascript Program to Check if strings are rotations of each other or not | Set 2 Given two strings s1 and s2, check whether s2 is a rotation of s1. Examples: Input : ABACD, CDABAOutput : TrueInput : GEEKS, EKSGEOutput : TrueWe have discussed an approach in earlier post which handles substring match as a pattern. In this post, we will be going to use KMP algorithm's lps (longest 2 min read JavaScript Program to Check if a Given Square Matrix is an Identity Matrix Given a matrix, our task is to check if a given square matrix is an Identity Matrix. An identity matrix (unit matrix) is a square matrix in which the principal diagonal elements are all one and other elements in the matrix are zero. Example:Input:[ [ 1, 0, 0] , [0, 1, 0] , [0, 0, 1]] Output: True Be 3 min read Javascript Program to Find element at given index after a number of rotations An array consisting of N integers is given. There are several Right Circular Rotations of range[L..R] that we perform. After performing these rotations, we need to find element at a given index.Examples : Input : arr[] : {1, 2, 3, 4, 5} ranges[] = { {0, 2}, {0, 3} } index : 1 Output : 3 Explanation 4 min read Check if strings are rotations of each other or not | Set 2 Given two strings s1 and s2, check whether s2 is a rotation of s1. Examples: Input : ABACD, CDABA Output : True Input : GEEKS, EKSGE Output : True We have discussed an approach in earlier post which handles substring match as a pattern. In this post, we will be going to use KMP algorithm's lps (long 6 min read Like