To check if the rows of a matrix are circularly identical in
Java, you need to compare each row with every other row to determine if one is
a circular rotation of the other.
Here's a step-by-step approach to achieve this:
- Convert
each row to a string: This helps in easily performing rotation checks.
- Concatenate
the row string with itself: This allows for checking circular
rotations using substring checks.
- Compare
each row with every other row: Ensure that each row can be found as a
substring in the doubled version of every other row.
Java Code Implementation
Here's how you can implement this:
java
public class
CircularMatrixRows { // Function to check if two rows are
circular rotations of each other public static boolean
areCircularlyIdentical(int[] row1, int[] row2) { if (row1.length != row2.length) { return false; } // Convert rows to strings StringBuilder sb1 = new
StringBuilder(); StringBuilder sb2 = new
StringBuilder(); for (int i = 0; i < row1.length;
i++) {
sb1.append(row1[i]).append(",");
sb2.append(row2[i]).append(","); } String s1 = sb1.toString(); String s2 = sb2.toString(); // Double the first row string String doubled = s1 + s1; // Check if the second row string is
a substring of the doubled first row string return doubled.contains(s2); } // Function to check if all rows in the
matrix are circularly identical public static boolean
areAllRowsCircularlyIdentical(int[][] matrix) { if (matrix == null || matrix.length
== 0) { return false; } for (int i = 0; i < matrix.length;
i++) { for (int j = i + 1; j <
matrix.length; j++) { if
(!areCircularlyIdentical(matrix[i], matrix[j])) { return false; } } } return true; } public static void main(String[] args) { int[][] matrix1 = { {1, 2, 3, 4}, {4, 1, 2, 3}, {3, 4, 1, 2}, {2, 3, 4, 1} }; int[][] matrix2 = { {1, 2, 3, 4}, {4, 1, 2, 3}, {3, 4, 1, 2}, {2, 3, 1, 4} }; System.out.println("Matrix 1
rows are circularly identical: " +
areAllRowsCircularlyIdentical(matrix1)); System.out.println("Matrix 2
rows are circularly identical: " +
areAllRowsCircularlyIdentical(matrix2)); } } |
Explanation
- areCircularlyIdentical(int[]
row1, int[] row2):
- Converts
the rows to strings by appending their elements separated by commas.
- Doubles
the string representation of the first row.
- Checks
if the string representation of the second row is a substring of the
doubled first row string.
- areAllRowsCircularlyIdentical(int[][]
matrix):
- Checks
if the matrix is non-null and non-empty.
- Compares
each row with every other row using areCircularlyIdentical function.
- Returns
false if any pair of rows is not circularly identical, otherwise returns true.
- main:
- Demonstrates
the usage of the functions with two example matrices.
- Prints
whether the rows of each matrix are circularly identical.
Conclusion
This program checks if the rows of a matrix are circularly
identical by leveraging string manipulation and substring checks. Adjust the
matrix inputs in the main method to test with different matrices.
0 Comments