Given two n x n binary matrices mat and target, return trueif it is possible to makematequal totarget _byrotating _mat
_in90-degree increments , or _falseotherwise.

Input: mat =[[0,1],[1,0]], target =[[1,0],[0,1]]Output: trueExplanation: We can rotate mat 90 degrees clockwise to make mat equal target.

Input: mat =[[0,1],[1,1]], target =[[1,0],[0,1]]Output: falseExplanation: It is impossible to make mat equal to target by rotating mat.

Input: mat =[[0,0,0],[0,1,0],[1,1,1]], target =[[1,1,1],[0,1,0],[0,0,0]]Output: trueExplanation: We can rotate mat 90 degrees clockwise two times to make mat equal target.
A matrix can be rotated 0, 90, 180, or 270 degrees. For each rotation, check if the rotated matrix matches the target. If any rotation matches, return true.
classSolution {
publicbooleanfindRotation(int[][] mat, int[][] target) {
int n = mat.length;
for (int r = 0; r < 4; ++r) {
boolean ok =true;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if (mat[i][j]!= target[i][j]) ok =false;
if (ok) returntrue;
int[][] rot =newint[n][n];
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
rot[j][n-1-i]= mat[i][j];
mat = rot;
}
returnfalse;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
funfindRotation(mat: Array<IntArray>, target: Array<IntArray>): Boolean {
val n = mat.size
var m = mat.map { it.copyOf() }.toTypedArray()
repeat(4) {
if ((0 until n).all { i -> (0 until n).all { j -> m[i][j] == target[i][j] } }) returntrueval rot = Array(n) { IntArray(n) }
for (i in0 until n)
for (j in0 until n)
rot[j][n-1-i] = m[i][j]
m = rot
}
returnfalse }
}
1
2
3
4
5
6
7
8
9
classSolution:
deffindRotation(self, mat: list[list[int]], target: list[list[int]]) -> bool:
n = len(mat)
m = [row[:] for row in mat]
for _ in range(4):
if m == target:
returnTrue m = [[m[n-j-1][i] for j in range(n)] for i in range(n)]
returnFalse
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
impl Solution {
pubfnfind_rotation(mut mat: Vec<Vec<i32>>, target: Vec<Vec<i32>>) -> bool {
let n = mat.len();
for _ in0..4 {
if mat == target { returntrue; }
letmut rot =vec![vec![0; n]; n];
for i in0..n {
for j in0..n {
rot[j][n-1-i] = mat[i][j];
}
}
mat = rot;
}
false }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
findRotation(mat: number[][], target: number[][]):boolean {
constn=mat.length;
letm=mat.map(row=>row.slice());
for (letr=0; r<4; ++r) {
letok=true;
for (leti=0; i<n; ++i)
for (letj=0; j<n; ++j)
if (m[i][j] !==target[i][j]) ok=false;
if (ok) returntrue;
constrot= Array.from({length: n}, () => Array(n).fill(0));
for (leti=0; i<n; ++i)
for (letj=0; j<n; ++j)
rot[j][n-1-i] =m[i][j];
m=rot;
}
returnfalse;
}
}