python 3 solution based on backtracking
class Solution:
    def solveSudoku(self, board: List[List[str]]) -> None:
        """
        Do not return anything, modify board in-place instead.
        """
        def is_valid(x, y, n):
            for i in range(9):
                if board[x][i] == n and i != y:
                    return False
                #col checking
                if board[i][y] == n and i != x:
                    return False
            box_x = x - x%3
            box_y = y - y%3
            for x_ in range(box_x, box_x+3):
                for y_ in range(box_y, box_y+3):
                    if board[x_][y_] == n and (x_,y_) != (x,y):
                        return False
            return True
        def find_blank():
            for x in range(9):
                for y in range(9):
                    if board[x][y] == '.':
                        return (x,y)
            return None
        def solve():
            blank_pos = find_blank()
            if not blank_pos:
                return True
            x, y = blank_pos
            for num in range(1,10):
                _num = f"{num}"
                if is_valid(x, y, _num):
                    board[x][y] = _num
                    if solve():
                        return True
                    board[x][y] = "."
        solve()
        return board        
Comments (0)