Problem

Given two matrices, write a function to check whether the two matrices are identical or not. Matrices are considered identical if all the elements in the respective positions of the two matrices are the same.

Examples

Example 1:

Input:
matrix1 = [[1, 2, 3],
		[4, 5, 6],
		[7, 8, 9]]
matrix2 = [[1, 2, 3],
		[4, 5, 6],
		[7, 8, 9]]
Output: true

Explanation: All the elements in corresponding positions are the same.

Example 2:

Input:
matrix1 = [[1, 2, 3],
		[4, 5, 6],
		[7, 8, 9]]
matrix2 = [[1, 2, 3],
		[4, 5, 6],
		[7, 8, 0]]
Output: false

Explanation: The elements at the last position of the matrices differ (9 != 0).

Solution

Method 1 - Iteration

To check if two matrices are identical, we need to compare their elements at each corresponding position. If every element matches, the matrices are identical. Otherwise, they are not. Additionally, we must ensure that the dimensions of the two matrices are the same before comparing their elements.

Here is the approach:

  1. Dimension Check: First, ensure both matrices have the same dimensions. If the number of rows or columns differ, they cannot be identical, and we can return false immediately.
  2. Element-wise Comparison: If the dimensions match, iterate through each element of both matrices and compare them.
    • Use nested loops to traverse each element.
    • For each pair of corresponding elements, check if they are equal.
    • If any pair is found to be different, return false.
  3. All Elements Matched: If all elements in respective positions are the same after traversal, return true.

Code

Java
public class Solution {
    public boolean areIdentical(int[][] matrix1, int[][] matrix2) {
        // Check if the dimensions of the two matrices are the same
        if (matrix1.length != matrix2.length || matrix1[0].length != matrix2[0].length) {
            return false;
        }
        
        // Iterate through each element to compare
        int rows = matrix1.length;
        int cols = matrix1[0].length;
        
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (matrix1[i][j] != matrix2[i][j]) {
                    return false;
                }
            }
        }
        
        return true;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        
        int[][] matrix1 = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };
        int[][] matrix2 = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };
        System.out.println(solution.areIdentical(matrix1, matrix2)); // Output: true
        
        int[][] matrix3 = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };
        int[][] matrix4 = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 0}
        };
        System.out.println(solution.areIdentical(matrix3, matrix4)); // Output: false
    }
}
Python
class Solution:
    def are_identical(self, matrix1, matrix2):
        # Check if the dimensions of the two matrices are the same
        if len(matrix1) != len(matrix2) or len(matrix1[0]) != len(matrix2[0]):
            return False
        
        # Iterate through each element to compare
        rows = len(matrix1)
        cols = len(matrix1[0])
        
        for i in range(rows):
            for j in range(cols):
                if matrix1[i][j] != matrix2[i][j]:
                    return False
        
        return True

# Example usage
solution = Solution()

matrix1 = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]
matrix2 = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]
print(solution.are_identical(matrix1, matrix2))  # Output: True

matrix3 = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]
matrix4 = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 0]
]
print(solution.are_identical(matrix3, matrix4))  # Output: False

Complexity

  • ⏰ Time complexity: O(m * n), where m is the number of rows and n is the number of columns. We traverse every element of both matrices.
  • 🧺 Space complexity: O(1)