-2

I was trying to solve the leetcode Sudoku Solver Problem (Problem description here). But my code is only giving partial solution.

class Solution(object):
    def solveSudoku(self, board):
        """
        :type board: List[List[str]]
        :rtype: None Do not return anything, modify board in-place instead.
        """
        def isValid(board, row, col, c):
          nRow = 3*(row//3)
          nCol = 3*(col//3)

          for i in range(9):
            #Check Row
            if board[row][i]==c:
              return False
            #Check Column
            if board[i][col]==c:
              return False
            #Check Grid
            if board[nRow+(i//3)][nCol+(i%3)]==c:
              return False
          return True


        def solve(board):
          for i in range(len(board)):
            for j in range(len(board)):
              if board[i][j]==".":
                for c in ["1","2","3","4","5","6","7","8","9"]:
                  if isValid(board, i, j, c): 
                    board[i][j]=c
                    if solve(board):
                        return True
                    board[i][j]=="."
                return False
          return True
        solve(board)
        
        
if __name__=='__main__':
  board1 = [["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"]]

  Solution().solveSudoku(board1)
  print(board1)
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Mousumi
  • 33
  • 6

1 Answers1

1

You are using a comparison operator (==) instead of assignment (=) when resetting the value of a position:

board[i][j]=="." 

should be:

board[i][j]="."
Alain T.
  • 40,517
  • 4
  • 31
  • 51
  • My bad! Thanks so much for the catch. I thought there are flaws with my logic and debugged there. – Mousumi Aug 21 '21 at 16:19