An n × n matrix is considered valid if every row and every column contains all the integers from 1 to n (inclusive).
You are given an n × n integer matrix matrix. Return True if the matrix is valid, and False otherwise.
Function Signature
def check_valid(matrix: list[list[int]]) -> bool:
...
Rules
-
matrix
is given as a list of
n
rows, and each row is a list of
n
integers.
-
Return a Python
bool
:
True
when every row and every column contains each of 1, 2, ...,
n
, and
False
otherwise.
Constraints
-
n == len(matrix) == len(matrix[i])
-
1 <= n <= 125
-
1 <= matrix[i][j] <= n
Examples
Input: matrix = [[1, 4, 2, 3], [3, 2, 4, 1], [4, 3, 1, 2], [2, 1, 3, 4]]
Output: True
Here n = 4, and every row and every column holds 1, 2, 3 and 4 exactly once, so the matrix is valid.
Input: matrix = [[2, 2, 2], [1, 3, 2], [3, 1, 1]]
Output: False
The first row is [2, 2, 2], which is missing 1 and 3. One failing row is enough to make the matrix invalid.
Input: matrix = [[1, 2], [1, 2]]
Output: False
Both rows contain 1 and 2, but the first column is [1, 1] and the second column is [2, 2], so the columns fail.