Programming Question: Leetcode-37(Sudoku Solver)
Approach to solve leetcode — 37 Sudoku Solver
- Sudoku is 9*9 board in which some numbers are filled already and some are to be filled by solver. For solving any sudoku, there are three rules:
a)Each of the digits 1-9
must occur exactly once in each row.
b)Each of the digits 1-9
must occur exactly once in each column.
c)Each of the digits 1-9
must occur exactly once in each of the 9 3x3
sub-boxes of the grid.
2. we will first create 3 arrays for rows, columns and boxes. The numbers which are filled already, we will place those numbers in their places on rows, columns and boxes as well.
3. Then we will create a backtrack function for filling each place of 9*9 board, Starting from 0,0 :
a)for base case, if we reach on last spot of 9*9 box, we will return.
b) we will see if the place is already filled, if it is filled then we will call the function recursively for next i and j.
c) if it is not filled , we will try filling numbers from 1 to 9 checking if number is not in related row, column and box and recursively call backtrack for next i and j.
Code:
