VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-rows-matrix-circular-rotations/

⇱ Check if all rows of a matrix are circular rotations of each other - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check if all rows of a matrix are circular rotations of each other

Last Updated : 23 Jul, 2025

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 :  

  1. 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.
  2. 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.
  3. Return true.

Below is the implementation of above steps. 


Output
Yes

Time complexity: O(n3
Auxiliary Space: O(n), since n extra space has been taken.

Comment
Article Tags: