Java Program to Check if all Rows of a Matrix are Circular Rotations of Each Other


Matrix consists of rows and columns to form a rectangular array. And circular rotations mean rotating the array's elements so that one rotation places the last element in the first position and the rest of the elements to the right. In this problem, we have given a matrix of n * n, and our task is to check if all rows of a matrix are circular rotations of each other then print “YES” otherwise print “NO”.

Let's see examples with explanations below to understand the problem in a better way.

Input 1

mat = [ [ 1, 5, 6],
      [ 5, 6, 1],
      [ 6, 1, 5] ]

Output 1

YES

Explanation

As consider the first row as a given array [1, 5, 6] now check for the remaining rows [5, 6, 1] and [6, 1, 5]:

Rotations for the second row [5, 6, 1] are: [1, 5, 6] is matched to the first row.

Rotations for the third row [6, 1, 5] are: [5, 6, 1], [1, 5, 6] is matched to the first row.

Therefore the answer is “YES” as all rows are permutations of each other.

Input 2

mat = [ [ 0, 9, 8],
    [ 9, 0, 8] ]

Output

NO

Explanation

As consider the first row as a given array [0, 9, 8] now check for the remaining rows [9, 0, 8]:

Rotations for the second row [9, 0, 8] are: [8, 9, 0] and [0, 8, 9], none of them are equal to the given array.

Therefore, the answer is “NO” as all rows are not permutations of each other.

Approaches

We have seen the example above for the given matrix, let us move to the approaches:

Approach1: Naive Approach

This approach idea is that we store the first row as a string and then combine the string with itself to enable the use of this string for substring search operations to perform. After that traverse the matrix and check the remaining rows, convert rows into a string then check, if the string is a substring of the first row's string (perform substring operation) return true otherwise return false.

Let's see the code below for a better understanding of the above approach.

Example

Below is a Java program to check if all rows of a matrix are circular rotations of each other

public class Rotations{ 
   // Function to check every row are circular rotations to each  other or not
   static boolean circularRotations(int mat[][], int n) {
      // create variable strFirstRow to store first  row's number and initialize it with empty string
      String strFirstRow = "";       
      // Traverse the matrix to store first row as string
      for (int i = 0; i < n; i++) {
         // add the first row numbers to the varible strFirstRow
         strFirstRow = strFirstRow + "-" + String.valueOf(mat[0][i]);
      } 
      // Combining the strFirstRow with strFirstRow to enable the use of this string for substring search operations
      strFirstRow = strFirstRow + strFirstRow; 
      // Traverse the matrix from 1 row to check all remaning rows
      for (int i = 1; i < n; i++) {
         // intialize variable strCurrentRow
         String strCurrentRow = "";
         // Traverse the curret row
         for (int j = 0; j < n; j++) {
            // Add current rows number to the variable strCurrentRow
            strCurrentRow = strCurrentRow + "-" + String.valueOf(mat[i][j]);
         } 
         // if the strCurrentRow is not present in the combining string strFirstRow
         if (strFirstRow.contentEquals(strCurrentRow)) {
            return false;
         }
      }
      // Returns true if every row of given matrix is circular rotations of each other
        return true;
   } 
   public static void main(String[] args)  {
      int n = 4;  // Given size of Matrix
      // Given Matrix
      int mat[][] = {{1, 5, 6, 9},
      {9, 1, 5, 6},
      {6, 9, 1, 5},
      {5, 6, 9, 1}
      };        
      // call function circularRotations to check every row of mat are circular rotations of each other or not
      if (circularRotations(mat, n)) {
            System.out.println("YES" + ", all rows of the matrix are circular rotations of each other");
      } else {
           System.out.println("NO" + ", all rows of the matrix are not circular rotations of each other");
      }
   }
}

Output

YES, all rows of the matrix are circular rotations of each other

Time and Space Complexity

The time complexity of the above code is O(N^3), as we traverse the matrix and use the content equal function.

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 Java Program to Check if all rows of a matrix are circular rotations of each other. We have implemented an approach in which convert rows into strings and use a content-equal function. The time complexity is O(N^3) and the space complexity of O(N). Where N is the size of the row and column of the matrix.

Updated on: 11-Jul-2023

95 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements