Determine Whether Matrix Can Be Obtained By Rotation (LeetCode 1886): Solution and Explanation
Given two n x n binary matrices mat and target, decide whether some number of 90-degree rotations turns mat into target.
Intuition
Rotating a square by 90 degrees four times returns it to the start, so there are only four orientations to test: 0, 90, 180 and 270 degrees.
That makes the problem a loop of at most four comparisons. The only real work is rotating the matrix between them.
A clockwise quarter turn can be done in place with two simple moves: transpose the matrix across its main diagonal, then reverse every row.
Approach: Rotate in place and compare
- Repeat four times.
- If
matequalstargetcell for cell, return true. - Otherwise transpose
mat: for everyi < j, swapmat[i][j]withmat[j][i]. - Reverse each row. Together with the transpose this rotates
mat90 degrees clockwise. - If none of the four orientations matched, return false.
Step-by-step example
mat = [[0,1],[1,0]], target = [[1,0],[0,1]]
- At 0 degrees, mat is [[0,1],[1,0]]. All four cells differ from target, so this orientation fails.
- Transpose: the matrix is symmetric, so it stays [[0,1],[1,0]].
- Reverse each row: [[1,0],[0,1]]. mat is now turned 90 degrees clockwise.
- Compare again: every cell matches target, so return true.
Code: Python, Java, C++
1class Solution:
2 def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:
3 n = len(mat)
4 for _ in range(4):
5 if mat == target:
6 return True
7 for i in range(n):
8 for j in range(i + 1, n):
9 mat[i][j], mat[j][i] = mat[j][i], mat[i][j]
10 for row in mat:
11 row.reverse()
12 return False1class Solution {
2 public boolean findRotation(int[][] mat, int[][] target) {
3 int n = mat.length;
4 for (int k = 0; k < 4; k++) {
5 if (same(mat, target)) {
6 return true;
7 }
8 for (int i = 0; i < n; i++) {
9 for (int j = i + 1; j < n; j++) {
10 int t = mat[i][j];
11 mat[i][j] = mat[j][i];
12 mat[j][i] = t;
13 }
14 }
15 for (int i = 0; i < n; i++) {
16 for (int l = 0, r = n - 1; l < r; l++, r--) {
17 int t = mat[i][l];
18 mat[i][l] = mat[i][r];
19 mat[i][r] = t;
20 }
21 }
22 }
23 return false;
24 }
25
26 private boolean same(int[][] a, int[][] b) {
27 for (int i = 0; i < a.length; i++) {
28 for (int j = 0; j < a.length; j++) {
29 if (a[i][j] != b[i][j]) return false;
30 }
31 }
32 return true;
33 }
34}1class Solution {
2public:
3 bool findRotation(vector<vector<int>>& mat, vector<vector<int>>& target) {
4 int n = mat.size();
5 for (int k = 0; k < 4; k++) {
6 if (mat == target) {
7 return true;
8 }
9 for (int i = 0; i < n; i++) {
10 for (int j = i + 1; j < n; j++) {
11 swap(mat[i][j], mat[j][i]);
12 }
13 }
14 for (auto& row : mat) {
15 reverse(row.begin(), row.end());
16 }
17 }
18 return false;
19 }
20};Time and space complexity
O(n^2)
Each of the at most four rounds compares all n^2 cells and rotates them once, so the total is a constant multiple of n^2.
O(1)
The rotation swaps cells in place, so no second matrix is needed.
Other approaches
Four flags in one pass (time O(n^2), space O(1))
After k clockwise turns, cell (i, j) of mat lands at a known spot: (i, j), (j, n-1-i), (n-1-i, n-1-j) or (n-1-j, i). Walk mat once and clear a flag whenever the cell disagrees with target at that spot. Return true if any flag survives. This avoids mutating the input.
Build rotated copies (time O(n^2), space O(n^2))
Create a new rotated matrix with rotated[j][n-1-i] = mat[i][j] each round. It is easy to read but allocates a fresh n x n grid every time.
Common mistakes
- Rotating counterclockwise instead of clockwise is harmless here, because all four orientations are tested either way, but mixing the two inside one rotation (transpose plus column reverse plus row reverse) gives a 180 degree flip instead of a turn.
- Transposing with both loops running from 0 swaps every pair twice and leaves the matrix unchanged. Start the inner loop at
i + 1. - A mirror image is not a rotation. A matrix reversed left to right usually cannot be reached by turning it.
- Comparing before the first rotation matters: when mat already equals target the answer is true with zero turns.
Frequently asked questions
Why only four orientations?
Four quarter turns make a full 360 degree turn, which puts every cell back where it began. Any longer sequence of rotations repeats one of the first four orientations.
Why does transpose plus row reverse rotate the matrix?
The transpose sends (i, j) to (j, i). Reversing the row then sends column i to column n-1-i, so the cell ends at (j, n-1-i), which is exactly where a clockwise quarter turn puts it.
Can I avoid changing mat?
Yes. Use four boolean flags and the index formulas for each rotation, checking every cell of mat against the matching cell of target in one pass.