easy Toeplitz Matrix
Problem Statement
Given an m x n matrix, determine if a given matrix is a Toeplitz matrix.
A Toeplitz matrix is one in which every diagonal from top-left to bottom-right has the same elements.
In other words, the matrix should have the property that each element is equal to the element diagonally down to its right.
Example 1:
- Input:
[[1,2,3],
[4,1,2],
[5,4,1]]
- Expected Output: true
- Justification: All diagonals have the same elements. Diagonals of the above matrix are [5], [4, 4], [1, 1, 1], [2, 2], and [3].
Example 2:
- Input:
[[1,2],
[3,4]]
- Expected Output: false
- Justification: The diagonal are [3], [1, 4], and [2]. A diagonal [1, 4] doesn't have the same elements.
Example 3:
- Input:
[[7,7,7],
[7,7,7],
[7,7,7]]
- Expected Output: true
- Justification: Each diagonal, although containing the same repeated element (7), satisfies the condition of a Toeplitz matrix.
Try it yourself
Try solving this question here:
🎯 STRICT STANDOUT — Toeplitz Matrix easy
0. Why / judgment (K3)
Family: Matrix diagonal invariant · local neighbor check
Toeplitz ⇔ matrix[i][j] == matrix[i-1][j-1] for all valid i,j. Checking every cell against its upper-left neighbor verifies all diagonals without grouping by (i-j) key. Constant extra space.
1. Pattern + recognition + when-NOT (K12)
Pattern / template: for i in 1..m-1: for j in 1..n-1: if A[i][j]!=A[i-1][j-1]: false. Recognition: constant-on-diagonal / shift-invariance of rows.
When-NOT: Not full sort of diagonals. Not BFS. If diagonals must be unique across different keys → different. Hankel is the anti-diagonal analogue — don’t confuse.
2. Complexity derivation (K11)
Visit Θ(m·n) cells once → O(mn) time (optimal to verify).
Space O(1).
3. Edge hand-run (K13)
[[1,2,3],[4,1,2],[5,4,1]]: check (1,1)=1==1, (1,2)=2==2, (2,1)=4==4, (2,2)=1==1 → true.
[[1,2],[3,4]]: (1,1)=4!=1 → false.
1×n or m×1: no interior checks → true.
Empty: true by vacuity.
4. Interviewer follow-ups & drills
Q1. Why check only upper-left neighbor?
Model answer: Transitivity along diagonal via chain of equalities.
Q2. Streamed rows?
Model answer: Keep previous row only — still O(n) space online.
Q3. Mis-tag as DP?
Model answer: No optimal substructure needed; local predicate.
✅ Solution Toeplitz Matrix
Problem Statement
Given an m x n matrix, determine if the given matrix is a Toeplitz matrix.
A Toeplitz matrix is one in which every diagonal from top-left to bottom-right has the same elements.
In other words, the matrix should have the property that each element is equal to the element diagonally down to its right.
Example 1:
- Input:
[[1,2,3],
[4,1,2],
[5,4,1]]
- Expected Output: true
- Justification: All diagonals have the same elements. Diagonals of the above matrix are [5], [4, 4], [1, 1, 1], [2, 2], and [3].
Example 2:
- Input:
[[1,2],
[3,4]]
- Expected Output: false
- Justification: The diagonal are [3], [1, 4], and [2]. A diagonal [1, 2] doesn't have the same elements.
Example 3:
- Input:
[[7,7,7],
[7,7,7],
[7,7,7]]
- Expected Output: true
- Justification: Each diagonal, although containing the same repeated element (7), satisfies the condition of a Toeplitz matrix.
Solution
To solve this problem, we will iterate through the elements of the matrix, checking if each element (except those in the last row and last column) matches with its diagonal successor. This approach works because a Toeplitz matrix is defined by the consistency of elements along its diagonals. By verifying this condition, we can ensure whether the matrix adheres to the Toeplitz criteria.
Step-by-step Algorithm
- Iterate over each row of the matrix, except the last row.
- For each row, iterate over each column, except the last column.
- Check if the current element is equal to the element diagonally down to its right.
- If any such pair of elements is not equal, return false immediately.
- For each row, iterate over each column, except the last column.
- If the entire matrix is iterated without finding any mismatch, return true.
Algorithm Walkthrough
Using Example 1: matrix =
[[1,2,3],
[4,1,2],
[5,4,1]]
- Start with the first element
(1)at matrix[0][0]. It matches with its diagonal successor(1)at matrix[1][1]. Continue. - Move to the second element
(2)in the first row at matrix[0][1]. It matches with its diagonal successor(2)at matrix[1][2]. Continue. - Skip the last element
(3)in the first row as it has no diagonal successor. - Move to the first element
(4)in the second row. It matches with its diagonal successor(4). Continue. - Next, Move to the second element
(1)in the second row. It matches with its diagonal successor(1). Continue. - Skip the last element
(2)in the second row. - Since we've reached the last row, we conclude that the matrix is a Toeplitz matrix.
Code
public class Solution {
// Method to check if a matrix is a Toeplitz matrix
public boolean isToeplitzMatrix(int[][] matrix) {
for (int i = 0; i < matrix.length - 1; i++) {
for (int j = 0; j < matrix[0].length - 1; j++) {
// Check if current element is equal to its diagonal successor
if (matrix[i][j] != matrix[i + 1][j + 1]) {
return false;
}
}
}
return true;
}
// Main method to test the algorithm
public static void main(String[] args) {
Solution solution = new Solution();
// Test case 1
System.out.println(
solution.isToeplitzMatrix(
new int[][] { { 1, 2, 3 }, { 4, 1, 2 }, { 5, 4, 1 } }
)
); // true
// Test case 2
System.out.println(
solution.isToeplitzMatrix(new int[][] { { 1, 2 }, { 3, 4 } })
); // false
// Test case 3
System.out.println(
solution.isToeplitzMatrix(
new int[][] { { 7, 7, 7 }, { 7, 7, 7 }, { 7, 7, 7 } }
)
); // true
}
}
Complexity Analysis
- Time Complexity:
, where mis the number of rows andnis the number of columns in the matrix. Each element is checked once. - Space Complexity:
, as no extra space is used besides a few variables for iteration.
🎯 STRICT STANDOUT — Solution Toeplitz Matrix
0. Why / judgment (K3)
Family: Matrix diagonal invariant
Local check is complete: if every consecutive pair on a diagonal matches, whole diagonal constant. Implementation double loop from (1,1). Avoid map-of-diagonals unless streaming constraints force hashing by i-j.
1. Pattern + recognition + when-NOT (K12)
Pattern / template: Nested loops; compare to upper-left; return false on first mismatch.
When-NOT: Don’t extract each diagonal into arrays (extra space, same O(mn)).
2. Complexity derivation (K11)
T=Θ(mn), S=Θ(1).
3. Edge hand-run (K13)
Hand-run 2×2 fail case [[1,2],[3,4]]: only check i=j=1: 4 vs 1 → false.
All equal values: all checks pass → true.
Single cell: true.
4. Interviewer follow-ups & drills
Q1. i-j key approach complexity?
Model answer: Still O(mn) time; O(m+n) space for first-seen map.
Q2. Follow-up: modify one cell to fix?
Model answer: Search mismatches — different problem.
Q3. Row-shift characterization?
Model answer: Each row is previous row shifted right by 1 with new first entry.
Recognize it: Grid traversal / rotation / in-place marking → index arithmetic, or DFS/BFS over cells.
▶ Visualize this problem (step it, predict each fork)
🤖 Don't fully get this? Learn it with Claude
Stuck on Toeplitz Matrix? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Progressively stronger hints — you still solve it.
I'm working on the problem **Toeplitz Matrix** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
See the technique, not just code.
Explain the optimal approach to **Toeplitz Matrix** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **Toeplitz Matrix**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **Toeplitz Matrix**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.