-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode.com problems sudoku-solver.ts
64 lines (53 loc) · 1.76 KB
/
leetcode.com problems sudoku-solver.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
63
64
/**
Do not return anything, modify board in-place instead.
*/
// function solveSudoku(board: string[][]): void {
// };
function solveSudoku(board: string[][]): void {
function backtrack(): boolean {
for (let row = 0; row < 9; row++) {
for (let col = 0; col < 9; col++) {
if (board[row][col] === '.') { // Find an empty cell
for (let num = 1; num <= 9; num++) {
const numStr = num.toString();
if (isValid(board, row, col, numStr)) {
board[row][col] = numStr; // Try this number
if (backtrack()) { // Recursively try to solve
return true;
}
board[row][col] = '.'; // Backtrack if no solution
}
}
return false; // No valid number found, backtrack
}
}
}
return true; // If all cells are filled correctly
}
backtrack();
}
function isValid(board: string[][], row: number, col: number, num: string): boolean {
// Check row
for (let c = 0; c < 9; c++) {
if (board[row][c] === num) {
return false;
}
}
// Check column
for (let r = 0; r < 9; r++) {
if (board[r][col] === num) {
return false;
}
}
// Check 3x3 subgrid
const startRow = Math.floor(row / 3) * 3;
const startCol = Math.floor(col / 3) * 3;
for (let r = startRow; r < startRow + 3; r++) {
for (let c = startCol; c < startCol + 3; c++) {
if (board[r][c] === num) {
return false;
}
}
}
return true;
}