Watch 10 video solutions for Determine Whether Matrix Can Be Obtained By Rotation, a easy level problem involving Array, Matrix. This walkthrough by codestorywithMIK has 6,198 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given two n x n binary matrices mat and target, return true if it is possible to make mat equal to target by rotating mat in 90-degree increments, or false otherwise.
Example 1:
Input: mat = [[0,1],[1,0]], target = [[1,0],[0,1]] Output: true Explanation: We can rotate mat 90 degrees clockwise to make mat equal target.
Example 2:
Input: mat = [[0,1],[1,1]], target = [[1,0],[0,1]] Output: false Explanation: It is impossible to make mat equal to target by rotating mat.
Example 3:
Input: mat = [[0,0,0],[0,1,0],[1,1,1]], target = [[1,1,1],[0,1,0],[0,0,0]] Output: true Explanation: We can rotate mat 90 degrees clockwise two times to make mat equal target.
Constraints:
n == mat.length == target.lengthn == mat[i].length == target[i].length1 <= n <= 10mat[i][j] and target[i][j] are either 0 or 1.Problem Overview: You are given two n x n matrices: mat and target. The task is to determine whether rotating mat by 0°, 90°, 180°, or 270° clockwise can make it exactly equal to target. Each rotation transforms element positions while preserving matrix size.
Approach 1: Rotate and Compare (Time: O(n2), Space: O(1))
Simulate the rotation process directly and compare the matrix with target after each rotation. A 90° clockwise rotation maps mat[i][j] to mat[j][n-1-i]. Rotate the matrix up to four times and after each rotation iterate through all cells to check if the matrices match. Each comparison takes O(n^2), and since there are at most four rotations, the overall complexity remains O(n^2). This approach works well because the rotation count is constant, so the algorithm stays efficient even for larger matrices.
Approach 2: Precompute 4 Rotations (Time: O(n2), Space: O(n2))
Instead of modifying the original matrix, generate all four rotated versions and compare each with target. Compute the 90°, 180°, and 270° transformations using index mapping while storing results in temporary matrices. After generating each rotation, perform a full matrix comparison. This approach is slightly easier to reason about because every rotation is stored explicitly, but it uses additional memory for the intermediate matrices.
Both approaches rely on understanding how indices move during a 90° clockwise rotation in a square matrix. This pattern frequently appears in problems involving matrix manipulation and array traversal, especially when dealing with grid transformations or image rotations.
Recommended for interviews: The rotate-and-compare approach is typically preferred. It demonstrates that you understand the coordinate transformation for matrix rotation and can implement it efficiently with constant extra space. Precomputing rotations is still acceptable and can help when you want clearer logic during implementation.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Rotate and Compare | O(n^2) | O(1) | Best general solution when you want minimal memory usage and direct rotation simulation. |
| Precompute 4 Rotations | O(n^2) | O(n^2) | Useful when clearer logic is preferred or when storing rotated matrices simplifies comparison. |