Leetcode 1886: Determine Whether Matrix Can Be Obtained By Rotation

grid47
grid47
Exploring patterns and algorithms
May 2, 2024 6 min read

You are given two n x n binary matrices mat and target. Your task is to determine if it is possible to rotate the matrix mat in 90-degree increments so that it matches the target matrix.
Problem
Approach
Steps
Complexity
Input: You are given two 2D matrices, mat and target, where each matrix is of size n x n. The elements of the matrices are binary (either 0 or 1).
Example: For example, given mat = [[0,1],[1,0]] and target = [[1,0],[0,1]]
Constraints:
• The number of rows and columns in mat and target is between 1 and 10, inclusive.
• Each element in mat and target is either 0 or 1.
Output: Return true if it is possible to rotate mat in 90-degree increments such that it becomes equal to target; otherwise, return false.
Example: For mat = [[0,1],[1,0]] and target = [[1,0],[0,1]], the output will be true.
Constraints:
• The output is a boolean value.
Goal: The goal is to check if mat can be rotated in 90-degree increments to match the target matrix.
Steps:
• Start with the matrix mat.
• For each 90-degree rotation, compare mat with target.
• If any of the rotations match target, return true. If none match, return false.
Goal: The matrix size n is constrained to values between 1 and 10.
Steps:
• 1 <= n <= 10
• mat[i][j] and target[i][j] are either 0 or 1.
Assumptions:
• The input matrices are always square matrices (n x n).
• The elements of the matrices are binary (0 or 1).
Input: mat = [[0,1],[1,0]] and target = [[1,0],[0,1]]
Explanation: By rotating mat 90 degrees clockwise, it matches the target matrix. Thus, the output is true.

Input: mat = [[0,1],[1,1]] and target = [[1,0],[0,1]]
Explanation: No matter how many times mat is rotated, it will never match the target matrix. Thus, the output is false.

Input: mat = [[0,0,0],[0,1,0],[1,1,1]] and target = [[1,1,1],[0,1,0],[0,0,0]]
Explanation: By rotating mat twice (180 degrees), it matches the target matrix. Thus, the output is true.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus