Toeplitz Matrix - Problem

Given an m x n matrix, return true if the matrix is Toeplitz. Otherwise, return false.

A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements.

In other words, a matrix is Toeplitz if matrix[i][j] == matrix[i+1][j+1] for all valid i and j.

Input & Output

Example 1 — Valid Toeplitz Matrix
$ Input: matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]]
Output: true
💡 Note: Every diagonal from top-left to bottom-right has the same elements: diagonal [1,1,1], diagonal [2,2,2], diagonal [3,3], diagonal [4], diagonal [5,5], diagonal [9]
Example 2 — Invalid Toeplitz Matrix
$ Input: matrix = [[1,2],[2,2]]
Output: false
💡 Note: The diagonal from top-left has elements [1,2] which are not all the same, so it's not a Toeplitz matrix
Example 3 — Single Element Matrix
$ Input: matrix = [[1]]
Output: true
💡 Note: A single element matrix is always Toeplitz since there are no diagonals to violate the property

Constraints

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

Visualization

Tap to expand
Toeplitz Matrix Problem Overview123451239512Check diagonals: [1,1,1], [2,2,2], [3,3], [4], [5,5], [9]All diagonals have identical elementsOutput: true
Understanding the Visualization
1
Input Matrix
3x4 matrix with elements to be checked
2
Diagonal Check
Verify each diagonal has same elements
3
Result
Return true if all diagonals are uniform
Key Takeaway
🎯 Key Insight: A matrix is Toeplitz if matrix[i][j] == matrix[i+1][j+1] for all valid positions
Asked in
Google 15 Facebook 12
180.0K Views
Medium Frequency
~15 min Avg. Time
2.1K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen