Validate a Latin-Square Matrix
Problem statement
Given a nonempty square integer matrix of size N × N, return whether every row and every column contains each integer from 1 through N exactly once.
For this exercise, assume the row and column conditions must both hold. This follows the reported examples: a matrix whose rows are all [1,2,3] is invalid because its columns repeat values.
Values outside [1,N] make the matrix invalid. The matrix need not have any particular diagonal or arrangement beyond the row and column conditions.
Function
validateLatinSquare(matrix: int[][]) → booleanExamples
Example 1
matrix = [[1,3,2],[2,1,3],[3,2,1]]return = trueEvery row and column contains exactly 1, 2 and 3.
Example 2
matrix = [[1,2,3],[1,2,3],[1,2,3]]return = falseEach row is valid, but columns repeat one value and omit others.
Example 3
matrix = [[1000,2,0],[-1000,2,3],[1,2,3]]return = falseSeveral values lie outside 1 through 3, so the matrix is invalid.
Constraints
- For this exercise, assume
1 <= N <= 30. - The input has exactly
Nrows of exactlyNintegers. - For this exercise, assume each integer lies in
[-10^9,10^9].