From 6baac2a8117cdd96b5c14cf037e14e3a47ce422b Mon Sep 17 00:00:00 2001 From: Satyam Singh <111181853+Satyam-Singh-01@users.noreply.github.com> Date: Sun, 5 Oct 2025 01:26:14 +0530 Subject: [PATCH] Create Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. The '.' character indicates empty cells. --- LeetCode/Problems/Sudoku Solver | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 LeetCode/Problems/Sudoku Solver diff --git a/LeetCode/Problems/Sudoku Solver b/LeetCode/Problems/Sudoku Solver new file mode 100644 index 00000000..c0b68659 --- /dev/null +++ b/LeetCode/Problems/Sudoku Solver @@ -0,0 +1,27 @@ +class Solution: + def solveSudoku(self, board): + def solve(): + for row in range(9): + for col in range(9): + if board[row][col] == '.': + for num in '123456789': + if is_valid(row, col, num): + board[row][col] = num + if solve(): + return True + board[row][col] = '.' + return False + return True + + def is_valid(row, col, num): + for i in range(9): + if board[row][i] == num or board[i][col] == num: + return False + start_row, start_col = 3 * (row // 3), 3 * (col // 3) + for i in range(start_row, start_row + 3): + for j in range(start_col, start_col + 3): + if board[i][j] == num: + return False + return True + + solve()