Skip to main content
Back to problems
#1886
Easy Algorithms

Determine whether matrix can be obtained by rotation

Array Matrix
59.3% acceptance
Feb 25, 2026
1585
148
Given two n x n binary matrices mat and target, return true if mat can be made equal to target by rotating mat in 90-degree increments.

Solution

Rust
Time O(n * m)
Space O(n * m)
LeetCode
solution.rs
impl Solution {
  pub fn find_rotation(mat: Vec<Vec<i32>>, target: Vec<Vec<i32>>) -> bool {
    let n = mat.len();
    let mut m = mat.clone();
    for _ in 0..4 {
      if m == target { return true; }
      // Rotate 90 degrees clockwise: new[j][n-1-i] = old[i][j]
      let mut rotated = vec![vec![0; n]; n];
      for i in 0..n {
        for j in 0..n {
          rotated[j][n - 1 - i] = m[i][j];
        }
      }
      m = rotated;
    }
    false
  }
}