37. sudoku solver

Sudoku Solver - LeetCode

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.

Example 1:

Input: board = [[“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”]] Output: [[“5”,“3”,“4”,“6”,“7”,“8”,“9”,“1”,“2”],[“6”,“7”,“2”,“1”,“9”,“5”,“3”,“4”,“8”],[“1”,“9”,“8”,“3”,“4”,“2”,“5”,“6”,“7”],[“8”,“5”,“9”,“7”,“6”,“1”,“4”,“2”,“3”],[“4”,“2”,“6”,“8”,“5”,“3”,“7”,“9”,“1”],[“7”,“1”,“3”,“9”,“2”,“4”,“8”,“5”,“6”],[“9”,“6”,“1”,“5”,“3”,“7”,“2”,“8”,“4”],[“2”,“8”,“7”,“4”,“1”,“9”,“6”,“3”,“5”],[“3”,“4”,“5”,“2”,“8”,“6”,“1”,“7”,“9”]] Explanation: The input board is shown above and the only valid solution is shown below:

Constraints:

board.length == 9
board[i].length == 9
board[i][j] is a digit or '.'.
It is **guaranteed** that the input board has only one solution.
class Solution:
    def solveSudoku(self, board: List[List[str]]) -> None:
        """
        Do not return anything, modify board in-place instead.
        """
        row_visited = set()
        col_visited = set()
        box_visited = set()
        for row in range(9):
            for col in range(9):
                if board[row][col] != '.':
                    box = (col)//3*3+row//3
                    row_visited.add((row, board[row][col],))
                    col_visited.add((col, board[row][col],))
                    box_visited.add((box, board[row][col],))

        def helper(row, col, board):
            if len(row_visited) == 9*9: 
                return True
            next_row, next_col = (row, col+1) if col!=8 else (row+1, 0)
            if board[row][col] == '.':
                for i in range(1,10):
                    box = (col)//3*3+row//3
                    if (row,str(i),) not in row_visited and (col,str(i),) not in col_visited and (box, str(i),) not in box_visited:
                        board[row][col] = str(i)
                        row_visited.add((row,str(i),))
                        col_visited.add((col,str(i),))
                        box_visited.add((box, str(i),))
                        if helper(next_row, next_col, board): return True
                        board[row][col] = '.'
                        row_visited.remove((row, str(i),))
                        col_visited.remove((col, str(i),))
                        box_visited.remove((box, str(i),))

            else:
                return helper(next_row, next_col, board)
        helper(0, 0, board)