-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckEveryRowColumnContainsAllNumbers.java
More file actions
50 lines (41 loc) · 1.7 KB
/
CheckEveryRowColumnContainsAllNumbers.java
File metadata and controls
50 lines (41 loc) · 1.7 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
42
43
44
45
46
47
48
49
50
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
public class CheckEveryRowColumnContainsAllNumbers {
public boolean checkValid(int[][] matrix) {
HashMap<Integer, HashSet<Integer>> rowMap = new HashMap<>();
HashMap<Integer, HashSet<Integer>> colMap = new HashMap<>();
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix.length; col++) {
if (rowMap.containsKey(row) && rowMap.get(row).contains(matrix[row][col])
|| colMap.containsKey(col) && colMap.get(col).contains(matrix[row][col])) {
return false;
}
if (rowMap.containsKey(row)) {
HashSet<Integer> rowSet = rowMap.get(row);
rowSet.add(matrix[row][col]);
rowMap.replace(row, rowSet);
} else {
rowMap.put(row, new HashSet<>(List.of(matrix[row][col])));
}
if (colMap.containsKey(col)) {
HashSet<Integer> columnSet = colMap.get(col);
columnSet.add(matrix[row][col]);
colMap.replace(col, columnSet);
} else {
colMap.put(col, new HashSet<>(List.of(matrix[row][col])));
}
}
}
return true;
}
public static void main(String[] args) {
CheckEveryRowColumnContainsAllNumbers checkEvery = new CheckEveryRowColumnContainsAllNumbers();
int[][] matrix = {
{1, 1, 1},
{1, 2, 3},
{1, 2, 3}
};
System.out.println(checkEvery.checkValid(matrix));
}
}