DDSA Solutions

1886. Determine Whether Matrix Can Be Obtained By Rotation

Time: O(n²)
Space: O(1)

Problem Overview

Check if mat equals any rotation of target.

Intuition

Check if mat equals any rotation of target. Compare mat with target rotated 0, 90, 180, 270 degrees.

Algorithm

  1. 1Rotate mat up to 3 times (90-degree rotation: (i,j) -> (j, n-1-i)). Check equality after each rotation.

Common Pitfalls

  • 90-degree clockwise rotation: new[j][n-1-i] = old[i][j]. Or equivalently new[i][j] = old[n-1-j][i].
1886.cs
C#
// Approach: Try all 4 rotations; return true if any matches target matrix.
// Time: O(n²) Space: O(1)

public class Solution
{
    public bool FindRotation(int[][] mat, int[][] target)
    {
        for (int i = 0; i < 4; ++i)
        {
            if (DeepEquals(mat, target))
                return true;
            Rotate(mat);
        }
        
        return false;
    }

    private void Rotate(int[][] mat)
    {
        int n = mat.Length;
        for (int i = 0, j = n - 1; i < j; ++i, --j)
        {
            int[] temp = mat[i];
            mat[i] = mat[j];
            mat[j] = temp;
        }

        for (int i = 0; i < n; ++i)
        {
            for (int j = i + 1; j < n; ++j)
            {
                int temp = mat[i][j];
                mat[i][j] = mat[j][i];
                mat[j][i] = temp;
            }
        }
    }

    private bool DeepEquals(int[][] a, int[][] b)
    {
        if (a.Length != b.Length)
            return false;

        for (int i = 0; i < a.Length; i++)
        {
            if (a[i].Length != b[i].Length)
                return false;
            for (int j = 0; j < a[i].Length; j++)
            {
                if (a[i][j] != b[i][j])
                    return false;
            }
        }

        return true;
    }
}
Was this solution helpful?

Related Problems