Problem
- We’re given a 9x9 sudoku board. A sudoku board is considered valid if the following are satisfied:
- Each row must contain the digits
1-9
without duplicates. - Each column must contain the digits
1-9
without duplicates. - Each of the nine
3 x 3
sub-boxes of the grid must contain the digits1-9
without duplicates.
- Each row must contain the digits
- Our function should return
true
if the board is valid, otherwise returnfalse
. - A board does not have to be filled to be valid. Also, ”.” denotes an empty cell.
Example
Approach 1
- To solve this we can take advantage of integer division and the grid system we have. For instance lets say we need to check for duplicates in the
row, col = [2, 2]
. We can divide by three to get the outer index, giving us[0,0]
.key = (r/3, c/3)
- We can use a hash map to find duplicates and reduce our time complexity.
- To find duplicates in the row and columns we can simply just iterate over them using a hash map as well
Implementation
Complexity Analysis
- Time Complexity:
- Space Complexity: