2133. Check if Every Row and Column Contains All Numbers LeetCode Solution

In this guide, you will get 2133. Check if Every Row and Column Contains All Numbers LeetCode Solution with the best time and space complexity. The solution to Check if Every Row and Column Contains All Numbers problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Check if Every Row and Column Contains All Numbers solution in C++
  4. Check if Every Row and Column Contains All Numbers solution in Java
  5. Check if Every Row and Column Contains All Numbers solution in Python
  6. Additional Resources
2133. Check if Every Row and Column Contains All Numbers LeetCode Solution image

Problem Statement of Check if Every Row and Column Contains All Numbers

An n x n matrix is valid if every row and every column contains all the integers from 1 to n (inclusive).
Given an n x n integer matrix matrix, return true if the matrix is valid. Otherwise, return false.

Example 1:

Input: matrix = [[1,2,3],[3,1,2],[2,3,1]]
Output: true
Explanation: In this case, n = 3, and every row and column contains the numbers 1, 2, and 3.
Hence, we return true.

Example 2:

Input: matrix = [[1,1,1],[1,2,3],[1,2,3]]
Output: false
Explanation: In this case, n = 3, but the first row and the first column do not contain the numbers 2 or 3.
Hence, we return false.

Constraints:

n == matrix.length == matrix[i].length
1 <= n <= 100
1 <= matrix[i][j] <= n

Complexity Analysis

  • Time Complexity: O(n^2)
  • Space Complexity: O(n)

2133. Check if Every Row and Column Contains All Numbers LeetCode Solution in C++

class Solution {
 public:
  bool checkValid(vector<vector<int>>& matrix) {
    const int n = matrix.size();

    for (int i = 0; i < n; ++i) {
      bitset<101> row;
      bitset<101> col;
      for (int j = 0; j < n; ++j) {
        row[matrix[i][j]] = true;
        col[matrix[j][i]] = true;
      }
      if (min(row.contains(), col.contains()) < n)
        return false;
    }

    return true;
  }
};
/* code provided by PROGIEZ */

2133. Check if Every Row and Column Contains All Numbers LeetCode Solution in Java

class Solution {
  public boolean checkValid(int[][] matrix) {
    final int n = matrix.length;

    for (int i = 0; i < n; ++i) {
      Set<Integer> row = new HashSet<>();
      Set<Integer> col = new HashSet<>();
      for (int j = 0; j < n; ++j) {
        row.add(matrix[i][j]);
        col.add(matrix[j][i]);
      }
      if (Math.min(row.size(), col.size()) < n)
        return false;
    }

    return true;
  }
}
// code provided by PROGIEZ

2133. Check if Every Row and Column Contains All Numbers LeetCode Solution in Python

class Solution:
  def checkValid(self, matrix: list[list[int]]) -> bool:
    return all(min(len(set(row)), len(set(col))) == len(matrix)
               for row, col in zip(matrix, zip(*matrix)))
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.