777. Toeplitz Matrix
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to determine if a given m x n matrix is a Toeplitz matrix. A matrix is defined as Toeplitz if every diagonal from the top-left to the bottom-right consists of the same elements.
Solution
Toeplitz Matrix in Python
class Solution:
def isToeplitzMatrix(self, matrix: list[list[int]]) -> bool:
m = len(matrix)
n = len(matrix[0])
# A matrix with one row or one column is always Toeplitz.
# The loops below naturally handle this by not executing.
# Iterate through the matrix, starting from the second row and second column.
for r in range(1, m):
for c in range(1, n):
# Compare the current element with its top-left neighbor.
if matrix[r][c] != matrix[r-1][c-1]:
# If they are not equal, it's not a Toeplitz matrix.
return False
# If all top-left-to-bottom-right diagonal elements are the same,
# the matrix is Toeplitz.
return True
Toeplitz Matrix in Java
class Solution {
public boolean isToeplitzMatrix(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
// A matrix with one row or one column is always Toeplitz.
// The loops below naturally handle this by not executing.
// Iterate through the matrix, starting from the second row and second column.
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
// Compare the current element with its top-left neighbor.
if (matrix[i][j] != matrix[i - 1][j - 1]) {
return false; // If they are not equal, it's not a Toeplitz matrix.
}
}
}
// If all top-left-to-bottom-right diagonal elements are the same,
// the matrix is Toeplitz.
return true;
}
}
Toeplitz Matrix in C++
class Solution {
public:
bool isToeplitzMatrix(vector<vector<int>>& matrix) {
int m = matrix.size();
int n = matrix[0].size();
// A matrix with one row or one column is always Toeplitz.
// The loops below naturally handle this by not executing.
// Iterate through the matrix, starting from the second row and second column.
for (int i = 1; i < m; ++i) {
for (int j = 1; j < n; ++j) {
// Compare the current element with its top-left neighbor.
if (matrix[i][j] != matrix[i - 1][j - 1]) {
return false; // If they are not equal, it's not a Toeplitz matrix.
}
}
}
// If all top-left-to-bottom-right diagonal elements are the same,
// the matrix is Toeplitz.
return true;
}
};
Toeplitz Matrix in TypeScript
function isToeplitzMatrix(matrix: number[][]): boolean {
const m = matrix.length;
const n = matrix[0].length;
// A matrix with one row or one column is always Toeplitz.
// The loops below naturally handle this by not executing.
// Iterate through the matrix, starting from the second row and second column.
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
// Compare the current element with its top-left neighbor.
if (matrix[i][j] !== matrix[i - 1][j - 1]) {
return false; // If they are not equal, it's not a Toeplitz matrix.
}
}
}
// If all top-left-to-bottom-right diagonal elements are the same,
// the matrix is Toeplitz.
return true;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.