Show problems as
Classic problem #766EasyArrayMatrix

Toeplitz Matrix

Real-world version: Banded Woven Panel
Tracked in this browser only
Read Toeplitz Matrix on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A loom produces a rectangular panel of coloured squares, handed to you as matrix, where matrix[i][j] is the colour code woven into row i, column j.

A band of the panel is traced by starting on some square and stepping repeatedly one row down and one column to the right, until the next step would leave the panel. Every square of the panel lies on exactly one band.

The panel is cleanly banded when all the squares of each band carry the same colour code. Return true when the panel is cleanly banded and false otherwise.

Examples

Example 1

Input
matrix = [[3, 7, 1], [4, 3, 7], [9, 4, 3]]
Output
true

The bands hold the colour codes 3, 3, 3 then 7, 7 then 1 then 4, 4 then 9, and every band carries a single code.

Example 2

Input
matrix = [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
Output
false

The band starting at row 0, column 0 visits the codes 1, 2 and 3, which are not all equal, so the panel is not cleanly banded.

Example 3

Input
matrix = [[99, 98], [0, 99]]
Output
true

One band holds 99 and 99, and the remaining two bands hold a single square each, so every band is uniform.

Constraints

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 20
  • 0 <= matrix[i][j] <= 99

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def is_toeplitz_matrix(matrix: list[list[int]]) -> bool:
Java
public boolean isToeplitzMatrix(int[][] matrix)