A ceramics workshop fires glazed tiles on a square kiln rack with n x n slots arranged in a grid. Every slot holds a shelf tagged with an integer firing-profile label between 1 and n. A firing cycle is considered uniform only when every row of the rack contains each profile label from 1 to n exactly once, and every column of the rack also contains each profile label from 1 to n exactly once. Given the label grid recorded for one firing cycle, determine whether that cycle was uniform.
n, the number of rows (and columns) in the grid.n lines contains n space-separated integers, where the j-th integer on the i-th of these lines is grid[i][j], the profile label of the slot in row i, column j (rows and columns are simply listed top-to-bottom, left-to-right in the input).Print a single line containing exactly true if every row and every column of the grid contains each integer from 1 to n exactly once, or false otherwise.
Example 1
Input
3 1 2 3 3 1 2 2 3 1
Expected
true
Explanation
Every row - (1,2,3), (3,1,2), (2,3,1) - is a permutation of 1..3, and every column - (1,3,2), (2,1,3), (3,2,1) - is also a permutation of 1..3, so the firing cycle is uniform and the answer is true.
Example 2
Input
3 1 1 2 1 2 3 2 3 3
Expected
false
Explanation
The first row is (1,1,2): it repeats profile 1 twice and never uses profile 3, so it is not a permutation of 1..3. The first column, (1,1,2), has the same problem. Because at least one row (and column) fails to contain every label exactly once, the cycle is not uniform and the answer is false.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →