-
Notifications
You must be signed in to change notification settings - Fork 0
/
037_SudokuSolver37.java
130 lines (111 loc) · 3.78 KB
/
037_SudokuSolver37.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
class Solution {
public boolean isValidSudoku(char[][] board) {
//check rows and columns
for (int i = 0; i < board.length; i++) {
HashSet<Character> h = new HashSet<>();
HashSet<Character> hh = new HashSet<>();
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] != '.' && h.contains(board[i][j])) {
return false;
}
if (board[j][i] != '.' && hh.contains(board[j][i])) {
return false;
}
h.add(board[i][j]);
hh.add(board[j][i]);
}
}
//check 3*3 matric
for (int k = 0; k < board.length; k = k + 3)
for (int l = 0; l < board.length; l = l + 3) {
if (!check(board, k, l)) {
return false;
}
}
return true;
}
public boolean check(char[][] chh, int row, int col) {
HashSet<Character> h = new HashSet<>();
for (int i = 0 + row; i < 3 + row; i++) {
for (int j = 0 + col; j < 3 + col; j++) {
if (chh[i][j] != '.' && h.contains(chh[i][j])) {
return false;
}
h.add(chh[i][j]);
}
}
return true;
}
}
************************************************************************************
class Solution {
public boolean isValidSudoku(char[][] board) {
List<boolean[]> subBoxCheckList = fillBooleanList(3);
List<boolean[]> colmsCheckList = fillBooleanList(9);
boolean[] row;
for(int i = 0; i < board.length; i++){
row = new boolean[10];
for(int j = 0; j < board.length; j++){
if(board[i][j] != '.') {
if(row[board[i][j] - 48]) {
return false;
}
else row[board[i][j] - 48] = true;
if(colmsCheckList.get(j)[board[i][j] - 48]) {
return false;
}
else colmsCheckList.get(j)[board[i][j] - 48] = true;
int subBoxIndex = (j / 3);
if(subBoxCheckList.get(subBoxIndex)[board[i][j] - 48]) {
return false;
}
else {
subBoxCheckList.get(subBoxIndex)[board[i][j] - 48] = true;
}
}
}
if((i + 1) % 3 == 0) subBoxCheckList = fillBooleanList(3);
}
return true;
}
private List<boolean[]> fillBooleanList(int size){
List<boolean[]> list = new ArrayList<>();
while(size != 0){
list.add(new boolean[10]);
size--;
}
return list;
}
}
***************************************************************
class Solution {
public boolean isValidSudoku(char[][] board) {
HashSet<Character> rowSet = new HashSet<>();
HashSet<Character> colSet = new HashSet<>();
HashSet<String> boxSet = new HashSet<>();
for (int rowCnt = 0; rowCnt < board.length; rowCnt++) {
for (int colCnt = 0; colCnt < board[rowCnt].length; colCnt++) {
if (board[rowCnt][colCnt] != '.') {
if (!rowSet.contains(board[rowCnt][colCnt]))
rowSet.add(board[rowCnt][colCnt]);
else
return false;
if (!boxSet.contains(rowCnt/3 + "-" + colCnt/3 + "-" + board[rowCnt][colCnt]))
boxSet.add(rowCnt/3 + "-" + colCnt/3 + "-" + board[rowCnt][colCnt]);
else
return false;
}
if (board[colCnt][rowCnt] != '.') {
if (colSet.contains(board[colCnt][rowCnt]))
return false;
else
colSet.add(board[colCnt][rowCnt]);
}
}
rowSet.clear();
colSet.clear();
}
return true;
}
}
******************************************************