import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class MatrixRotationCheck {
// Function to check whether another matrix can be created by rotating
// mat one or more times by 90 degrees
public static String findRotation(int[][] mat, int[][] T) {
// If the dimensions of both arrays don't match
if (T.length != mat.length || T[0].length != mat[0].length) {
// Return "No"
return "No";
}
// Map to store all rows, columns, and their reversed versions
Map<String, Integer> m = new HashMap<>();
// Iterate through the matrix
for (int[] row : mat) {
// Increment the frequency of the row and its reverse by 1
String rowStr = Arrays.toString(row);
m.put(rowStr, m.getOrDefault(rowStr, 0) + 1);
int[] reversedRow = Arrays.copyOf(row, row.length);
for (int i = 0, j = row.length - 1; i < j; i++, j--) {
int temp = reversedRow[i];
reversedRow[i] = reversedRow[j];
reversedRow[j] = temp;
}
String reversedRowStr = Arrays.toString(reversedRow);
m.put(reversedRowStr, m.getOrDefault(reversedRowStr, 0) + 1);
}
// Iterate through the columns of the matrix
for (int j = 0; j < mat[0].length; j++) {
int[] column = new int[mat.length];
for (int i = 0; i < mat.length; i++) {
column[i] = mat[i][j];
}
// Increment the frequency of the column and its reverse by 1
String columnStr = Arrays.toString(column);
m.put(columnStr, m.getOrDefault(columnStr, 0) + 1);
int[] reversedColumn = Arrays.copyOf(column, column.length);
for (int i = 0, k = column.length - 1; i < k; i++, k--) {
int temp = reversedColumn[i];
reversedColumn[i] = reversedColumn[k];
reversedColumn[k] = temp;
}
String reversedColumnStr = Arrays.toString(reversedColumn);
m.put(reversedColumnStr, m.getOrDefault(reversedColumnStr, 0) + 1);
}
// Iterate through the target matrix T
for (int[] row : T) {
// If the frequency of the row is less in T than in mat
String rowStr = Arrays.toString(row);
if (m.getOrDefault(rowStr, 0) <= 0) {
return "No";
}
// Decrement the frequency of the row by 1
m.put(rowStr, m.get(rowStr) - 1);
}
// Iterate through the columns of the target matrix T
for (int j = 0; j < T[0].length; j++) {
int[] column = new int[T.length];
for (int i = 0; i < T.length; i++) {
column[i] = T[i][j];
}
// If the frequency of the column is less in T than in mat
String columnStr = Arrays.toString(column);
if (m.getOrDefault(columnStr, 0) <= 0) {
return "No";
}
// Decrement the frequency of the column by 1
m.put(columnStr, m.get(columnStr) - 1);
}
// Return "Yes"
return "Yes";
}
// Driver code
public static void main(String[] args) {
// Input matrices
int[][] mat = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int[][] T = {
{3, 6, 9},
{2, 5, 8},
{1, 4, 7}
};
// Function call
System.out.println(findRotation(mat, T));
}
}