-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0036-valid-sudoku.java
73 lines (64 loc) · 2.27 KB
/
0036-valid-sudoku.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
73
class Solution {
public boolean isValidSudoku(char[][] board) {
//neetcode solution, slightly modified
//a set of the characters that we have already come across (excluding '.' which denotes an empty space)
Set<Character> rowSet = null;
Set<Character> colSet = null;
for (int i = 0; i < 9; i++) {
//reinitialize the sets so we don't carry over found characters from the previous run
rowSet = new HashSet<>();
colSet = new HashSet<>();
for (int j = 0; j < 9; j++) {
char r = board[i][j];
char c = board[j][i];
if (r != '.'){
if (rowSet.contains(r)){
return false;
} else {
rowSet.add(r);
}
}
if (c != '.'){
if (colSet.contains(c)){
return false;
} else {
colSet.add(c);
}
}
}
}
//block
//loop controls advance by 3 each time to jump through the boxes
for (int i = 0; i < 9; i = i + 3) {
for (int j = 0; j < 9; j = j + 3) {
//checkBlock will return true if valid
if (!checkBlock(i, j, board)) {
return false;
}
}
}
//passed all tests, therefore valid board
return true;
}
public boolean checkBlock(int idxI, int idxJ, char[][] board) {
Set<Character> blockSet = new HashSet<>();
//if idxI = 3 and indJ = 0
//rows = 6 and cols = 3
int rows = idxI + 3;
int cols = idxJ + 3;
//and because i initializes to idxI but only goes to rows, we loop 3 times (once for each row)
for (int i = idxI; i < rows; i++) {
//same for columns
for (int j = idxJ; j < cols; j++) {
if (board[i][j] == '.') {
continue;
}
if (blockSet.contains(board[i][j])) {
return false;
}
blockSet.add(board[i][j]);
}
}
return true;
}
}