Formatted question description: https://leetcode.ca/all/2133.html

2133. Check if Every Row and Column Contains All Numbers

  • Difficulty: Easy.
  • Related Topics: Array, Hash Table, Matrix.
  • Similar Questions: Valid Sudoku, Matrix Diagonal Sum.

Problem

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

Solution (Java, C++, Python)

  • class Solution {
        public boolean checkValid(int[][] matrix) {
            int n = matrix.length;
            Set<Integer> set = new HashSet<>();
            for (int[] ints : matrix) {
                for (int anInt : ints) {
                    set.add(anInt);
                }
                if (set.size() != n) {
                    return false;
                }
                set.clear();
            }
            for (int i = 0; i < matrix[0].length; i++) {
                for (int[] ints : matrix) {
                    set.add(ints[i]);
                }
                if (set.size() != n) {
                    return false;
                }
                set.clear();
            }
            return true;
        }
    }
    
  • Todo
    
  • # 2133. Check if Every Row and Column Contains All Numbers
    # https://leetcode.com/problems/check-if-every-row-and-column-contains-all-numbers/
    
    class Solution:
        def checkValid(self, matrix: List[List[int]]) -> bool:
            n = len(matrix)
            
            for row in matrix:
                if len(set(row)) != n:
                    return False
            
            for col in zip(*matrix):
                if len(set(col)) != n:
                    return False
            
            return True
            
            
    
    

Explain:

nope.

Complexity:

  • Time complexity : O(n).
  • Space complexity : O(n).

All Problems

All Solutions