-
Notifications
You must be signed in to change notification settings - Fork 16
/
ValidSudoku.java
72 lines (64 loc) · 2.2 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
70
71
72
// https://leetcode.com/problems/valid-sudoku
// T: O(1)
// S: O(1)
import java.util.HashSet;
import java.util.Set;
public class ValidSudoku {
private static final int SUDOKU_SIZE = 9;
public boolean isValidSudoku(char[][] board) {
if (repetitionInRows(board)) return false;
if (repetitionInColumns(board)) return false;
return !repetitionInBlocks(board);
}
private boolean repetitionInRows(char[][] board) {
for (char[] row : board) {
if (repetitionInRow(row)) return true;
}
return false;
}
private boolean repetitionInRow(char[] row) {
final Set<Character> digits = new HashSet<>();
for (char character : row) {
if (Character.isDigit(character)) {
if (digits.contains(character)) return true;
digits.add(character);
}
}
return false;
}
private boolean repetitionInColumns(char[][] board) {
final Set<Character> digits = new HashSet<>();
for (int column = 0 ; column < SUDOKU_SIZE ; column++) {
digits.clear();
for (int row = 0 ; row < SUDOKU_SIZE ; row++) {
char c = board[row][column];
if (Character.isDigit(c)) {
if (digits.contains(c)) return true;
digits.add(c);
}
}
}
return false;
}
private boolean repetitionInBlocks(char[][] board) {
for (int i = 0 ; i < SUDOKU_SIZE / 3 ; i++) {
for (int j = 0 ; j < SUDOKU_SIZE / 3 ; j++) {
if (repetitionInBlock(board, i, j)) return true;
}
}
return false;
}
private boolean repetitionInBlock(final char[][] board, final int i, final int j) {
final Set<Character> digits = new HashSet<>();
for (int row = 3 * i ; row < 3 * i + 3 ; row++) {
for (int column = 3 * j ; column < 3 * j + 3 ; column++) {
char c = board[row][column];
if (Character.isDigit(c)) {
if (digits.contains(c)) return true;
digits.add(c);
}
}
}
return false;
}
}