Determine Whether Matrix Can Be Obtained By Rotation (LeetCode 1886): Solution and Explanation

Easy
Array
Matrix

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

  1. Repeat four times.
  2. If mat equals target cell for cell, return true.
  3. Otherwise transpose mat: for every i < j, swap mat[i][j] with mat[j][i].
  4. Reverse each row. Together with the transpose this rotates mat 90 degrees clockwise.
  5. If none of the four orientations matched, return false.

Step-by-step example

mat = [[0,1],[1,0]], target = [[1,0],[0,1]]
  1. At 0 degrees, mat is [[0,1],[1,0]]. All four cells differ from target, so this orientation fails.
  2. Transpose: the matrix is symmetric, so it stays [[0,1],[1,0]].
  3. Reverse each row: [[1,0],[0,1]]. mat is now turned 90 degrees clockwise.
  4. 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 False
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

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.

Space

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.