Suppose we have a matrix M, we have to check whether it is a Toeplitz matrix or not. As we know a matrix is said to be Toeplitz when every diagonal descending from left to right has the same value.
So, if the input is like
then the output will be True.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution: def solve(self, matrix): for i in range(len(matrix)-1): for j in range(len(matrix)-1): if matrix[i][j]!=matrix[i+1][j+1]: return False return True ob = Solution() matrix = [ [7, 2, 6], [3, 7, 2], [5, 3, 7]] print(ob.solve(matrix))
[[7, 2, 6], [3, 7, 2], [5, 3, 7]]