-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidSudoku.js
130 lines (109 loc) · 3.52 KB
/
validSudoku.js
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
130
// 1.
function isValidSudoku(board) {
let columnNums;
let subBox1 = [], subBox2 = [], subBox3 = [], subBox4 = [], subBox5 = [], subBox6 = [], subBox7 = [], subBox8 = [], subBox9 = [];
for(let i = 0; i < board.length; i++) {
columnNums = [];
for (let j = 0; j < board[0].length; j++) {
// row check
if (board[i][j] !== '.' && board[i].indexOf(board[i][j]) !== j) return false;
// column check
if (columnNums.some((e) => {
return e === board[j][i]
})) return false;
else if (board[j][i] !== '.') columnNums.push(board[j][i]);
// subBox check for every 3 lines
if (i < 3 && j < 3 && board[i][j] !== '.') {
if (subBox1) {
if (subBox1.some((e) => {
return e === board[i][j]
})) return false;
else subBox1.push(board[i][j]);
}
}
if (i < 3 && j >= 3 && j < 6 && board[i][j] !== '.') {
if (subBox2.some((e) => {
return e === board[i][j]
})) return false;
else subBox2.push(board[i][j]);
}
if (i < 3 && j >= 6 && j < 9 && board[i][j] !== '.') {
if (subBox3.some((e) => {
return e === board[i][j]
})) return false;
else subBox3.push(board[i][j]);
}
if (i >= 3 && i < 6 && j < 3 && board[i][j] !== '.') {
if (subBox4.some((e) => {
return e === board[i][j]
})) return false;
else subBox4.push(board[i][j]);
}
if (i >= 3 && i < 6 && j >= 3 && j < 6 && board[i][j] !== '.') {
if (subBox5.some((e) => {
return e === board[i][j]
})) return false;
else subBox5.push(board[i][j]);
}
if (i >= 3 && i < 6 && j > 6 && j < 9 && board[i][j] !== '.') {
if (subBox6.some((e) => {
return e === board[i][j]
})) return false;
else subBox6.push(board[i][j]);
}
if (i >= 6 && i < 9 && j < 3 && board[i][j] !== '.') {
if (subBox7.some((e) => {
return e === board[i][j]
})) return false;
else subBox7.push(board[i][j]);
}
if (i >= 6 && i < 9 & j >= 3 && j < 6 && board[i][j] !== '.') {
if (subBox8.some((e) => {
return e === board[i][j]
})) return false;
else subBox8.push(board[i][j]);
}
if (i >= 6 && i < 9 && j >= 6 && j < 9 && board[i][j] !== '.') {
if (subBox9.some((e) => {
return e === board[i][j]
})) return false;
else subBox9.push(board[i][j]);
}
}
}
return true;
}
// 2.
// var isValidSudoku = function(board) {
// let row = 9;
// let column = 9;
// // row check;
// for (let i = 0 ; i < column ; i++) {
// var rowSet = new Set();
// for (let j = 0 ; j < row ; j++) {
// if (board[i][j] != "." && !rowSet.has(board[i][j])) rowSet.add(board[i][j]);
// else if (rowSet.has(board[i][j])) return false;
// }
// }
// // column check;
// for (let j = 0 ; j < column ; j++) {
// var columnSet = new Set();
// for (let i = 0 ; i < row ; i++) {
// if (board[i][j] != "." && !columnSet.has(board[i][j])) columnSet.add(board[i][j]);
// else if (columnSet.has(board[i][j])) return false;
// }
// }
// // 3x3 check;
// for (let a = 0 ; a < column ; a += 3) {
// for (let b = 0 ; b < row ; b += 3) {
// var squareSet = new Set();
// for (let i = 0 ; i < 3 ; i++) {
// for (let j = 0 ; j < 3 ; j++) {
// if (board[a+i][b+j] != "." && !squareSet.has(board[a+i][b+j])) squareSet.add(board[a+i][b+j]);
// else if (squareSet.has(board[a+i][b+j])) return false;
// }
// }
// }
// }
// return true;
// };