Check if Every Row and Column Contains All Numbers

Published: Sep 27, 2022

Easy Matrix Array


The problem is about n x n matrix and 1 to n (inclusive) values. Using a set data structure, check cells of each row and columns doesn’t duplicate.

Problem Description

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.


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


Example 1
Input: matrix = [[1,2,3],[3,1,2],[2,3,1]]
Output: true
Example 2
Input: matrix = [[1,1,1],[1,2,3],[1,2,3]]
Output: false


The solution uses a set for horizontal and array of sets for vertical. When a row is scanned, it checks the horizontal set. When all rows are scanned, it checks the array of vertical sets. If all are length n, return True.


class CheckAllNumbers:
    def checkValid(self, matrix: List[List[int]]) -> bool:
        n = len(matrix)
        vertical = [set() for _ in range(n)]
        for i in range(n):
            horizontal = set()
            for j in range(n):
                cur = matrix[i][j]
            if len(horizontal) != n:
                return False
        for i in range(n):
            if len(vertical[i]) != n:
                return False
        return True


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