09 Sept 2026Go / Python / TypeScriptEasy

Toeplitz Matrix

Check whether every top-left to bottom-right matrix diagonal contains one repeated value.

Every cell outside the first row and column must equal its immediate top-left neighbor, which directly tests the Toeplitz invariant.

complexity

O(rows * columns) time and O(1) extra space.

solution files

  • Go toeplitz-matrix/solution.go
  • Python toeplitz-matrix/solution.py
  • TypeScript toeplitz-matrix/solution.ts

Solution files

Gotoeplitz-matrix/solution.go
package main

func isToeplitzMatrix(matrix [][]int) bool {
	for row := 1; row < len(matrix); row++ {
		for column := 1; column < len(matrix[0]); column++ {
			if matrix[row][column] != matrix[row-1][column-1] {
				return false
			}
		}
	}
	return true
}
Pythontoeplitz-matrix/solution.py
class Solution:
    def isToeplitzMatrix(self, matrix: list[list[int]]) -> bool:
        return all(matrix[row][column] == matrix[row - class="syntax-number">1][column - class="syntax-number">1] for row in range(class="syntax-number">1, len(matrix)) for column in range(class="syntax-number">1, len(matrix[class="syntax-number">0])))
TypeScripttoeplitz-matrix/solution.ts
function isToeplitzMatrix(matrix: number[][]): boolean {
  for (let row = class="syntax-number">1; row < matrix.length; row += class="syntax-number">1) {
    for (let column = class="syntax-number">1; column < matrix[class="syntax-number">0].length; column += class="syntax-number">1) if (matrix[row][column] !== matrix[row - class="syntax-number">1][column - class="syntax-number">1]) return false;
  }
  return true;
}