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:


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 <= 251 <= mat[i].length <= 251 <= mat[i][j] <= 251 <= k <= 50When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for this problem involves checking every possible way to shift each row of the matrix. We generate all possible shifted versions of each row and then compare the matrix with all these shifted combinations to see if any are similar.
Here's how the algorithm would work step-by-step:
def are_matrices_similar_brute_force(matrix_a, matrix_b):
number_of_rows = len(matrix_a)
number_of_columns = len(matrix_a[0])
def shift_row(row, shift_amount):
shifted_row = row[:] # Create a copy to avoid modifying the original
for i in range(shift_amount):
last_element = shifted_row.pop()
shifted_row.insert(0, last_element)
return shifted_row
def compare_matrices(matrix_one, matrix_two):
for row_index in range(number_of_rows):
if matrix_one[row_index] != matrix_two[row_index]:
return False
return True
# Generate all possible shift combinations.
shift_combinations = []
def generate_combinations(current_combination, row_index):
if row_index == number_of_rows:
shift_combinations.append(current_combination[:])
return
for shift_amount in range(number_of_columns):
current_combination.append(shift_amount)
generate_combinations(current_combination, row_index + 1)
current_combination.pop()
generate_combinations([], 0)
# Iterate through shift combinations and apply shifts
for shift_combination in shift_combinations:
# Apply shifts to matrix_a
shifted_matrix = []
for row_index in range(number_of_rows):
shifted_row = shift_row(matrix_a[row_index], shift_combination[row_index])
shifted_matrix.append(shifted_row)
# Compare shifted_matrix with matrix_b
if compare_matrices(shifted_matrix, matrix_b):
# Matrices are similar after shifts
return True
# No shift combination resulted in similar matrices
return FalseWe're trying to see if two sets of rows from two matrices are essentially the same, even if one set is shifted. The clever approach is to combine each row with itself, and then check if any row from the second matrix exists within this combined row.
Here's how the algorithm would work step-by-step:
def are_matrices_similar(matrix_one, matrix_two):
number_of_rows = len(matrix_one)
number_of_columns = len(matrix_one[0])
# Ensure matrices have the same dimensions.
if number_of_rows != len(matrix_two) or number_of_columns != len(matrix_two[0]):
return False
for row_index in range(number_of_rows):
first_matrix_row = matrix_one[row_index]
second_matrix_row = matrix_two[row_index]
# Create a doubled string to check for rotation.
concatenated_row = first_matrix_row + first_matrix_row
# Check if the second row is a substring of the doubled first row.
is_rotation_found = False
for start_index in range(len(concatenated_row) - number_of_columns + 1):
if concatenated_row[start_index:start_index + number_of_columns] == second_matrix_row:
is_rotation_found = True
break
# If any row isn't a rotation, matrices aren't similar.
if not is_rotation_found:
return False
return True| Case | How to Handle |
|---|---|
| matrix is null or empty | Return true, as an empty matrix can be considered to have all rows equal. |
| matrix contains an empty row | Treat an empty row as equal to other rows if all rows are empty, otherwise determine similarity based on non-empty rows only. |
| matrix has only one row | Return true, as a single row is trivially similar to itself. |
| All rows are identical | Return true, as the matrix is already similar. |
| Rows have different lengths | Return false, as cyclic shifts cannot make rows of different lengths equal. |
| No solution exists (rows are fundamentally different) | The core logic should return false when no cyclic shift aligns the rows. |
| Large matrix with very long rows (performance) | Ensure the shift comparison logic is efficient (e.g., using string matching or optimized comparison). |
| Integer overflow during comparisons if values are large | Use appropriate data types or comparison methods to prevent overflow, if comparing sums or products. |