-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathCheckIfEveryRowAndEveryColumnContainAllNumbers.java
More file actions
41 lines (36 loc) · 1.23 KB
/
CheckIfEveryRowAndEveryColumnContainAllNumbers.java
File metadata and controls
41 lines (36 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.HashSet;
import java.util.Set;
public class CheckIfEveryRowAndEveryColumnContainAllNumbers {
private int n;
public boolean checkValid(int[][] matrix) {
this.n = matrix.length;
return checkAllRows(matrix) && checkAllColumns(matrix);
}
private boolean checkAllRows(int[][] matrix) {
for (int[] row : matrix) {
if (!isRowValid(row)) return false;
}
return true;
}
private boolean isRowValid(int[] row) {
final Set<Integer> numbers = new HashSet<>();
for (int element: row) {
if (element <= 0 || element > n) return false;
if (numbers.contains(element)) return false;
numbers.add(element);
}
return true;
}
private boolean checkAllColumns(int[][] matrix) {
for (int column = 0 ; column < n ; column++) {
final Set<Integer> numbers = new HashSet<>();
for (int row = 0 ; row < n ; row++) {
int element = matrix[row][column];
if (element < 0 || element > n) return false;
if (numbers.contains(element)) return false;
numbers.add(element);
}
}
return true;
}
}