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.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach involves trying out all possible rotations of the original matrix and comparing each rotated version to the target matrix. We essentially rotate the matrix in place 90 degrees at a time, checking for a match after each rotation. If none of the rotations match, the matrices are not obtainable through rotation.
Here's how the algorithm would work step-by-step:
def determine_if_matrix_can_be_obtained_by_rotation(matrix, target):
number_of_rows = len(matrix)
number_of_columns = len(matrix[0])
if matrix == target:
return True
rotated_matrix = [row[:] for row in matrix]
for _ in range(3):
# Rotate the matrix 90 degrees clockwise
rotated_matrix = list(zip(*rotated_matrix[::-1]))
#Check if the rotated matrix matches the target
if rotated_matrix == target:
return True
# If none of the rotations match, return False
return FalseThe trick is to realize that rotating a matrix four times brings it back to the original. Therefore, we only need to check three rotations to determine if the target matrix can be obtained. Each rotation involves rearranging elements in a specific way, which we can do by comparing each rotation with the target.
Here's how the algorithm would work step-by-step:
def determine_if_matrix_can_be_obtained_by_rotation(
matrix, target_matrix):
if matrix == target_matrix:
return True
for _ in range(3):
# Rotate the matrix 90 degrees clockwise
number_of_rows = len(matrix)
number_of_columns = len(matrix[0])
rotated_matrix = [([0] * number_of_rows) for _ in range(
number_of_columns)]
for i in range(number_of_rows):
for j in range(number_of_columns):
rotated_matrix[j][number_of_rows - 1 - i] = matrix[i][j]
matrix = rotated_matrix
# Check if the rotated matrix is equal to the target matrix
if matrix == target_matrix:
return True
# No rotation resulted in the target matrix
return False| Case | How to Handle |
|---|---|
| Null or empty matrix inputs | Return true if both are null/empty; otherwise, return false because rotation isn't applicable or possible. |
| Matrices with different dimensions (not square or different sizes) | Return false immediately because rotation is only defined for square matrices of the same size. |
| 1x1 matrix | Return true if the single element in both matrices is the same, as any rotation yields the same matrix. |
| 2x2 matrix | Explicitly test all four rotations (0, 90, 180, 270 degrees) as this is a small but crucial test case to check rotation logic. |
| Large matrix (performance considerations) | Ensure the rotation logic is efficient (e.g., in-place rotation or using loops with proper index manipulation) to avoid time limit exceeded errors for large input matrices. |
| Matrix containing only identical values | The rotation checks should still function correctly regardless of element values within the matrix. |
| Matrix containing negative, zero, and positive values | The comparison during rotation checks must handle the full range of integer values without type overflow errors. |
| Integer overflow during index calculations in large matrices | Use appropriate data types (e.g., long) for index calculations if the matrix dimensions could potentially lead to overflow. |