# Program to check whether given matrix is Toeplitz Matrix or not in Python

PythonServer Side ProgrammingProgramming

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

 7 2 6 3 7 2 5 3 7

then the output will be True.

To solve this, we will follow these steps −

• for each row i except the last one, do
• for each column except the last one, do
• if matrix[i, j] is not same as matrix[i+1, j+1], then
• return False
• return True

Let us see the following implementation to get better understanding −

## Example

Live Demo

class Solution:
def solve(self, matrix):
for i in range(len(matrix)-1):
for j in range(len(matrix[0])-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))

## Input

[[7, 2, 6],
[3, 7, 2],
[5, 3, 7]]

## Output

True
Published on 05-Oct-2020 07:40:30