-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidSudoku.java
69 lines (59 loc) · 2.93 KB
/
ValidSudoku.java
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.awt.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
public class ValidSudoku {
public boolean isValidSudoku(char[][] board) {
HashMap<Integer, HashSet<Integer>> rowMap = new HashMap<>();
HashMap<Integer, HashSet<Integer>> colMap = new HashMap<>();
HashMap<Point, HashSet<Integer>> square = new HashMap<>();
for (int row = 0; row < 9; row++) {
for (int column = 0; column < 9; column++) {
if (board[row][column] == '.') continue;
if (rowMap.containsKey(row) && rowMap.get(row).contains((int) board[row][column])
|| colMap.containsKey(column) && colMap.get(column).contains((int) board[row][column])
|| square.containsKey(new Point(row / 3, column / 3))
&& square.get(new Point(row / 3, column / 3)).contains((int) board[row][column])) {
return false;
}
if (rowMap.containsKey(row)) {
HashSet<Integer> rowSet = rowMap.get(row);
rowSet.add((int) board[row][column]);
rowMap.replace(row, rowSet);
} else {
rowMap.put(row, new HashSet<Integer>(List.of((int) board[row][column])));
}
if (colMap.containsKey(column)) {
HashSet<Integer> columnSet = colMap.get(column);
columnSet.add((int) board[row][column]);
colMap.replace(column, columnSet);
} else {
colMap.put(column, new HashSet<>(List.of((int) board[row][column])));
}
if (square.containsKey(new Point(row / 3, column / 3))) {
HashSet<Integer> squareSet = square.get(new Point(row / 3, column / 3));
squareSet.add((int) board[row][column]);
square.replace(new Point(row / 3, column / 3), squareSet);
} else {
square.put(new Point(row / 3, column / 3), new HashSet<>(List.of((int) board[row][column])));
}
}
}
return true;
}
public static void main(String[] args) {
ValidSudoku sudoku = new ValidSudoku();
char[][] sudokuBoard = {
{'.', '.', '.', '.', '5', '.', '.', '1', '.'},
{'.', '4', '.', '3', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '3', '.', '.', '1'},
{'8', '.', '.', '.', '.', '.', '.', '2', '.'},
{'.', '.', '2', '.', '7', '.', '.', '.', '.'},
{'.', '1', '5', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '2', '.', '.', '.'},
{'.', '2', '.', '9', '.', '.', '.', '.', '.'},
{'.', '.', '4', '.', '.', '.', '.', '.', '.'}
};
System.out.println(sudoku.isValidSudoku(sudokuBoard));
}
}