-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathValid Sudoku.js
More file actions
28 lines (19 loc) · 773 Bytes
/
Valid Sudoku.js
File metadata and controls
28 lines (19 loc) · 773 Bytes
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
var isValidSudoku = function(board) {
const rows = Array.from({ length: 9 }, () => new Set());
const cols = Array.from({ length: 9 }, () => new Set());
const boxes = Array.from({ length: 9 }, () => new Set());
for (let i = 0; i < 9; i++) {
for (let j = 0; j < 9; j++) {
const val = board[i][j];
if (val === '.') continue;
if (rows[i].has(val)) return false;
rows[i].add(val);
if (cols[j].has(val)) return false;
cols[j].add(val);
const boxIndex = Math.floor(i / 3) * 3 + Math.floor(j / 3);
if (boxes[boxIndex].has(val)) return false;
boxes[boxIndex].add(val);
}
}
return true;
};