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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code