27(June) Toeplitz matrix

27. Toeplitz Matrix

The problem can be found at the following link: Question Link

Problem Description

A Toeplitz (or diagonal-constant) matrix is a matrix in which each descending diagonal from left to right is constant, i.e., all elements in a diagonal are the same. Given a rectangular matrix mat, your task is to complete the function isToeplitz which returns 1 if the matrix is Toeplitz, otherwise it returns 0.

Example:

Input:

mat = [[6, 7, 8],
       [4, 6, 7],
       [1, 4, 6]]

Output: 1

Explanation: The test case represents a 3x3 matrix:

6 7 8
4 6 7
1 4 6

Output: 1 (True) as values in all downward diagonals from left to right contain the same elements.

Input:

mat = [[1, 2, 3],
       [4, 5, 6]]

Output: 0

Explanation: Matrix of order 2x3:

Output: 0 (False) as values in all diagonals are different.

My Approach

  1. Initialization:

  • Create a map to store the first element of each diagonal. The key will be the difference between the row and column indices (i - j).

  1. Matrix Traversal:

  • Iterate through each element in the matrix.

  • Calculate the diagonal key as i - j.

  • Check if the key already exists in the map.

    • If it exists, compare the current element with the stored value. If they differ, the matrix is not Toeplitz.

    • If it doesn't exist, store the current element in the map with the diagonal key.

  1. Return:

  • Return 1 if all diagonals have constant values, otherwise return 0.

Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(n * m), as we iterate through each matrix element.

  • Expected Auxiliary Space Complexity: O(n + m), where n is the number of rows and m is the number of columns, for storing the diagonals in the map.

Code

C++

Java

Python

Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


📍Visitor Count

Last updated