-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path36. Valid Sudoku(ts).ts
62 lines (54 loc) · 1.71 KB
/
36. Valid Sudoku(ts).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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Runtime 146 ms Beats 49.13%
* Memory 47.6 MB Beats 41.96%
*/
const isValid = (row: string[]): boolean => {
const cell = row.filter((cell) => cell !== '.');
const set = new Set(cell);
return set.size === cell.length;
};
const isRowValid = (board: string[][]): boolean =>
board.every((row) => isValid(row));
const isColumnValid = (board: string[][]) => {
const row: string[] = Array(9).fill('.');
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < board[0].length; j++) {
row[j] = board[j][i];
}
if (!isValid(row)) {
return false;
}
}
return true;
};
const is3on3Vaild = (board: string[][]): boolean => {
let newBoard: string[][] = Array.from({ length: board.length }, () => []);
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < 3; j++) {
newBoard[Math.floor(i / 3)].push(board[j][i]);
}
for (let j = 3; j < 6; j++) {
newBoard[Math.floor(i / 3) + 3].push(board[j][i]);
}
for (let j = 6; j < 9; j++) {
newBoard[Math.floor(i / 3) + 6].push(board[j][i]);
}
}
return isRowValid(newBoard);
};
function isValidSudoku(board: string[][]): boolean {
return isRowValid(board) && isColumnValid(board) && is3on3Vaild(board);
}
console.log(
isValidSudoku([
['5', '3', '.', '.', '7', '.', '.', '.', '.'],
['6', '.', '.', '1', '9', '5', '.', '.', '.'],
['.', '9', '8', '.', '.', '.', '.', '6', '.'],
['8', '.', '.', '.', '6', '.', '.', '.', '3'],
['4', '.', '.', '8', '.', '3', '.', '.', '1'],
['7', '.', '.', '.', '2', '.', '.', '.', '6'],
['.', '6', '.', '.', '.', '.', '2', '8', '.'],
['.', '.', '.', '4', '1', '9', '.', '.', '5'],
['.', '.', '.', '.', '8', '.', '.', '7', '9'],
])
); // true