Problem overview
The method isToeplitzMatrix gets matrix, an int[][], and returns true when every diagonal holds a single value. A diagonal starts in the top row or the left column and goes one row down and one column right at each step.
The grid with rows [7, 3, 8], [4, 7, 3], [6, 4, 7] and [1, 6, 4] has six diagonals: 1; 6, 6; 4, 4, 4; 7, 7, 7; 3, 3; and 8. Each repeats one value, so the answer is true. In the grid [5, 2, 9, 1], [0, 5, 2, 9], [3, 0, 5, 8], the diagonal that starts at the 2 in row 0 holds 2, 2 and 8, so the answer is false.
Approach
One way is to walk each diagonal from its starting cell and compare every value with the first one. That works, but it needs a loop over the starting cells plus a loop along each diagonal, and it is easy to get the bounds wrong.
There is a simpler view. A diagonal holds a single value exactly when every cell equals the cell diagonally up and to its left. If cell (i, j) matches (i - 1, j - 1) for every cell that has such a neighbor, then going along any diagonal each value equals the one before it, so they are all equal. This is a local check, so no diagonal needs to be tracked, and no extra memory is used.
The cells in row 0 and column 0 have no up-left neighbor. They only start diagonals, so they are never the cell being checked.
Step by step
- Store the number of rows in
mand the number of columns,matrix[0].length, inn. - Loop
ifrom 1 tom - 1. Row 0 is skipped because nothing is above it. - Loop
jfrom 1 ton - 1. Column 0 is skipped for the same reason. Together the two loops reach every cell that has an up-left neighbor, once. - If
matrix[i][j]differs frommatrix[i - 1][j - 1], returnfalseat once. One mismatch is enough to answer. - If the loops finish without a mismatch, return
true.
The solution in Java
class Solution {
public boolean isToeplitzMatrix(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (matrix[i][j] != matrix[i - 1][j - 1]) {
return false;
}
}
}
return true;
}
}
Comparing two int values with != compares their numbers, which is what you want here. (It would be a trap only with boxed Integer objects, and this lesson uses a plain int[][].) Because the loops start at 1, a grid with one row or one column never enters the inner body, so the method returns true straight away.
Complexity
Time is O(m * n), because each cell is compared once at most. Extra space is O(1): only m, n, i and j are stored, and the grid isn't copied.
Edge cases and common mistakes
- A single row or a single column. Every diagonal is one cell, so the answer is always
true. The grid[3],[8],[3]returnstrue, and so does[1, 2, 3, 4]. - Starting the loops at 0. Then
matrix[i - 1][j - 1]reads index-1and Java throwsArrayIndexOutOfBoundsException. - Comparing with the wrong neighbor. The up-left cell
matrix[i - 1][j - 1]is the one on the same diagonal. Comparing withmatrix[i - 1][j]would test columns, not diagonals. - Checking only the first diagonal misses mismatches elsewhere, as the second example shows: the 8 is in the last row.
Testing
Running the solution on the samples, and on the single-row grid [1, 2, 3, 4], prints:
true
false
true
true
The tests print booleans in lowercase, the same way.
