Search⌘ K
AI Features

Solution: Toeplitz Matrix

Understand how to verify if a matrix is a Toeplitz matrix by checking that every diagonal from top left to bottom right has identical elements. This lesson guides you through an efficient O(mn) algorithm with constant space, including edge cases and practical considerations for large matrices. You will learn to implement a solution that iterates through matrix cells, comparing each element to its diagonal neighbor, ensuring clear comprehension of the Toeplitz matrix property.

Statement

Given an m×nm \times n matrix, determine whether it is a Toeplitz matrix. Return TRUE if it is, otherwise return FALSE.

A matrix is considered Toeplitz if every diagonal running from the top left to the bottom right contains identical elements. In other words, for every cell matrix[i][j], if both i + 1 and j + 1 are within bounds, then matrix[i][j] must equal matrix[i+1][j+1].

Note:

  • What if the matrix is stored on disk and memory is limited such that you can only load at most one row of the matrix into memory at a time?

  • What if the matrix is so large that you can only load a partial row into memory at once?

Constraints:

  • mm ==== matrix.length

  • ...