-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
33 lines (26 loc) · 897 Bytes
/
index.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
let coords = [0, 0, 0, 3, 3, 3, 6, 6, 6];
export function isValidSudoku(board) {
let rows = [];
let cols = [];
let boxes = {};
for (let row = 0; row < board.length; row++) {
if (!rows[row]) rows[row] = {};
for (let col = 0; col < board[row].length; col++) {
if (!cols[col]) cols[col] = {};
const value = board[row][col];
if (value === '.') continue;
// row
if (rows[row][value]) return false;
rows[row][value] = true;
// col
if (cols[col][value]) return false;
cols[col][value] = true;
// box
const coord = String(coords[row]) + String(coords[col]);
if (!boxes[coord]) boxes[coord] = {};
if (boxes[coord][value]) return false;
boxes[coord][value] = true;
}
}
return true;
}