-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0036-valid-sudoku.ts
41 lines (34 loc) · 943 Bytes
/
0036-valid-sudoku.ts
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
function isValidSudoku(board: string[][]): boolean {
const rows = {};
const cols = {};
const squares = {};
for (let r = 0; r < 9; r++) {
for (let c = 0; c < 9; c++) {
const num = board[r][c];
if (num === '.') {
continue;
}
const grid = `${Math.floor(r / 3)}${Math.floor(c / 3)}`;
if (!cols[c]) {
cols[c] = new Set();
}
if (!rows[r]) {
rows[r] = new Set();
}
if (!squares[grid]) {
squares[grid] = new Set();
}
if (
rows[r].has(num) ||
cols[c].has(num) ||
squares[grid].has(num)
) {
return false;
}
cols[c].add(num);
rows[r].add(num);
squares[grid].add(num);
}
}
return true;
}