Similar Problems

Similar Problems not available

Determine Whether Matrix Can Be Obtained By Rotation - Leetcode Solution

Companies:

LeetCode:  Determine Whether Matrix Can Be Obtained By Rotation Leetcode Solution

Difficulty: Easy

Topics: matrix array  

Problem Statement: Given two 2D matrices A and B, determine whether one can obtain B by rotating A clockwise or anticlockwise and up-down reflection.

Example: Input: A = [[1,2],[3,4]], B = [[3,4],[1,2]] Output: true Explanation: We can rotate A 180 degrees clockwise, then get matrix B.

Approach: To solve this problem, we need to follow the following steps:

  1. Check if A and B have the same dimensions. If not, return false as it is not possible to obtain B from A by just rotation and reflection.
  2. Rotate the matrix 90 degrees clockwise and check if it matches with B. If it does, return true.
  3. Rotate the matrix 180 degrees clockwise and check if it matches with B. If it does, return true.
  4. Rotate the matrix 270 degrees clockwise and check if it matches with B. If it does, return true.
  5. Flip the matrix along its horizontal axis and repeat steps 2-4.
  6. Return false if none of the rotations and reflection match with B.

Code:

bool canBeObtainedByRotation(vector<vector<int>>& A, vector<vector<int>>& B) { if(A.size() != B.size() || A[0].size() != B[0].size()) { return false; } bool canMatch = false; int n = A.size(); int m = A[0].size(); vector<vector<int>> A_transpose(m, vector<int>(n));

for(int i = 0; i < n; i++) {
    for(int j = 0; j < m; j++) {
        A_transpose[j][i] = A[i][j]; // finding the transpose of A to rotate the matrix
    }
}

//90 degrees clockwise rotation
for(int i = 0; i < m; i++) {
    reverse(A_transpose[i].begin(), A_transpose[i].end());
}
if(A_transpose == B) {
    return true;
}

//180 degrees clockwise rotation
reverse(A_transpose.begin(), A_transpose.end());
for(int i = 0; i < m; i++) {
    reverse(A_transpose[i].begin(), A_transpose[i].end());
}
if(A_transpose == B) {
    return true;
}

//270 degrees clockwise rotation
reverse(A_transpose.begin(), A_transpose.end());
for(int i = 0; i < m; i++) {
    reverse(A_transpose[i].begin(), A_transpose[i].end());
}
if(A_transpose == B) {
    return true;
}

//flip along the horizontal axis
for(int i = 0; i < n/2; i++) {
    for(int j = 0; j < m; j++) {
        swap(A[i][j], A[n-i-1][j]);
    }
}

//90 degrees clockwise rotation after Flip
for(int i = 0; i < m; i++) {
    reverse(A_transpose[i].begin(), A_transpose[i].end());
}
if(A_transpose == B) {
    return true;
}

//180 degrees clockwise rotation after Flip
reverse(A_transpose.begin(), A_transpose.end());
for(int i = 0; i < m; i++) {
    reverse(A_transpose[i].begin(), A_transpose[i].end());
}
if(A_transpose == B) {
    return true;
}

//270 degrees clockwise rotation after Flip
reverse(A_transpose.begin(), A_transpose.end());
for(int i = 0; i < m; i++) {
    reverse(A_transpose[i].begin(), A_transpose[i].end());
}
if(A_transpose == B) {
    return true;
}

//no match found
return false;

}

Time Complexity: O(n*m), where n and m are the number of rows and columns of the matrix.

Space Complexity: O(n*m), where n and m are the number of rows and columns of the matrix.

Determine Whether Matrix Can Be Obtained By Rotation Solution Code

1