2946. Matrix Similarity After Cyclic Shifts LeetCode Solution

In this guide, you will get 2946. Matrix Similarity After Cyclic Shifts LeetCode Solution with the best time and space complexity. The solution to Matrix Similarity After Cyclic Shifts problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Matrix Similarity After Cyclic Shifts solution in C++
  4. Matrix Similarity After Cyclic Shifts solution in Java
  5. Matrix Similarity After Cyclic Shifts solution in Python
  6. Additional Resources
2946. Matrix Similarity After Cyclic Shifts LeetCode Solution image

Problem Statement of Matrix Similarity After Cyclic Shifts

You are given an m x n integer matrix mat and an integer k. The matrix rows are 0-indexed.
The following proccess happens k times:

Even-indexed rows (0, 2, 4, …) are cyclically shifted to the left.

Odd-indexed rows (1, 3, 5, …) are cyclically shifted to the right.

Return true if the final modified matrix after k steps is identical to the original matrix, and false otherwise.

Example 1:

Input: mat = [[1,2,3],[4,5,6],[7,8,9]], k = 4
Output: false
Explanation:
In each step left shift is applied to rows 0 and 2 (even indices), and right shift to row 1 (odd index).

Example 2:

Input: mat = [[1,2,1,2],[5,5,5,5],[6,3,6,3]], k = 2
Output: true
Explanation:

Example 3:

Input: mat = [[2,2],[2,2]], k = 3
Output: true
Explanation:
As all the values are equal in the matrix, even after performing cyclic shifts the matrix will remain the same.

Constraints:

1 <= mat.length <= 25
1 <= mat[i].length <= 25
1 <= mat[i][j] <= 25
1 <= k <= 50

Complexity Analysis

  • Time Complexity: O(mn)
  • Space Complexity: O(1)

2946. Matrix Similarity After Cyclic Shifts LeetCode Solution in C++

class Solution {
 public:
  bool areSimilar(vector<vector<int>>& mat, int k) {
    const int n = mat[0].size();
    for (const vector<int>& row : mat)
      for (int j = 0; j < n; ++j)
        if (row[j] != row[(j + k) % n])
          return false;
    return true;
  }
};
/* code provided by PROGIEZ */

2946. Matrix Similarity After Cyclic Shifts LeetCode Solution in Java

class Solution {
  public boolean areSimilar(int[][] mat, int k) {
    final int n = mat[0].length;
    for (int[] row : mat)
      for (int j = 0; j < n; ++j)
        if (row[j] != row[(j + k) % n])
          return false;
    return true;
  }
}
// code provided by PROGIEZ

2946. Matrix Similarity After Cyclic Shifts LeetCode Solution in Python

class Solution:
  def areSimilar(self, mat: list[list[int]], k: int) -> bool:
    n = len(mat[0])
    for row in mat:
      for j in range(n):
        if row[j] != row[(j + k) % n]:
          return False
    return True
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.