commit 347fd22ebc3f96b8d016363a0b998b87267c48ca
parent c2626ccd050dd852b59d63a97529978b1e4ade9a
Author: Jack Sangdahl <jack@pngu.org>
Date: Thu, 26 Mar 2026 20:54:17 -0600
2946. matrix similarity after cyclic shifts
Diffstat:
2 files changed, 72 insertions(+), 0 deletions(-)
diff --git a/matrix_similarity_cyclic_shift.c b/matrix_similarity_cyclic_shift.c
@@ -0,0 +1,44 @@
+// 2946. Matrix Similarity After Cyclic Shifts
+#include "leetcode.h"
+
+/*
+ * you are given an 'm x n' integer matrix 'mat' and an integer 'k'. the matrix
+ * rows are 0-indexed. the following process happens 'k' times: even indexed
+ * rows are cyclically shifted to the left. odd indexed rows 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.
+ */
+
+bool areSimilar(int **mat, int matSize, int *matColSize, int k) {
+ int m = matSize, n = matColSize[0];
+ k %= n;
+ for (int i = 0; i < m; i++)
+ for (int j = 0; j < n; j++)
+ if (mat[i][j] != mat[i][(j + k) % n])
+ return false;
+ return true;
+}
+
+int main() {
+ int m1i[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
+ int m2i[3][4] = {{1, 2, 1, 2}, {5, 5, 5, 5}, {6, 3, 6, 3}};
+ int m3i[2][2] = {{2, 2}, {2, 2}};
+ struct two_d_arr *m1 =
+ two_d_arr_init(ARRAY_SIZE(m1i), ARRAY_SIZE(m1i[0]), m1i);
+ struct two_d_arr *m2 =
+ two_d_arr_init(ARRAY_SIZE(m2i), ARRAY_SIZE(m2i[0]), m2i);
+ struct two_d_arr *m3 =
+ two_d_arr_init(ARRAY_SIZE(m3i), ARRAY_SIZE(m3i[0]), m3i);
+ bool r1 = areSimilar(m1->arr, m1->row_size, m1->col_size, 4);
+ bool r2 = areSimilar(m2->arr, m2->row_size, m2->col_size, 2);
+ bool r3 = areSimilar(m3->arr, m3->row_size, m3->col_size, 3);
+ printf("%d\n", r1);
+ assert(r1 == false);
+ printf("%d\n", r2);
+ assert(r2 == true);
+ printf("%d\n", r3);
+ assert(r3 == true);
+ two_d_arr_free(m1);
+ two_d_arr_free(m2);
+ two_d_arr_free(m3);
+}
diff --git a/matrix_similarity_cyclic_shift.py b/matrix_similarity_cyclic_shift.py
@@ -0,0 +1,28 @@
+# 2946. Matrix Similarity After Cyclic Shifts
+from typing import List
+
+"""
+you are given an 'm x n' integer matrix 'mat' and an integer 'k'. the matrix
+rows are 0-indexed. the following process happens 'k' times: even indexed
+rows are cyclically shifted to the left. odd indexed rows 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.
+"""
+
+
+class Solution:
+ def areSimilar(self, mat: List[List[int]], k: int) -> bool:
+ m, n = len(mat), len(mat[0])
+ k %= n
+ for i in range(m):
+ for j in range(n):
+ if mat[i][j] != mat[i][(j + k) % n]:
+ return False
+ return True
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.areSimilar(mat=[[1, 2, 3], [4, 5, 6], [7, 8, 9]], k=4))
+ print(obj.areSimilar(mat=[[1, 2, 1, 2], [5, 5, 5, 5], [6, 3, 6, 3]], k=2))
+ print(obj.areSimilar(mat=[[2, 2], [2, 2]], k=3))