Valid Sudoku Checker in Python
Problem statement: You have given sudoku as a matrix. For example,
If the below three conditions are valid then it’s valid sudoku.
- Each row will have items from 1 to 9.
- Each column will have items from 1 to 9.
- Each 3*3 cell (9 elements) will have items from 1 to 9.
Also, mention time and space complexity to validate sudoku.
This question was asked in the Protonn coding interview. This is one of the standard questions and is asked in many coding challenges for software developer job profiles.
Python Solution
Here we are implementing the sudoku matrix as a list of lists. Like,
sudoku = [[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,8]]
Some of the tricks we have used to solve this problem.
- We are using the built-in
set()
method to identify if all the elements in the list (sudoku row or sudoku column) are unique. Basically, theset()
method removes all the duplicate elements from the list. If the length of the set is 9, it means there is no duplicate element. - We are using the list comprehension technique to get the column elements.
[item[col_num] for item in sudoku]
- A slicing mechanism from the Python list is used to get the elements for each cell.
The rest of the code is self-explanatory even if you know the Python basics.
#validate row def isRowValid(row_num): return len(set(sudoku[row_num])) == 9 #validate column def isColValid(col_num): col = [item[col_num] for item in sudoku] return len(set(col)) == 9 #validate cell def isCelValid(cel_row, cel_col): vals = sudoku[cel_row][cel_col: cel_col+3] vals.extend(sudoku[cel_row+1] [cel_col: cel_col+3]) vals.extend(sudoku[cel_row+2] [cel_col: cel_col+3]) return len(set(vals)) == 9 #validate sudoku def validateSudoku(): for i in range(0,9): if not isRowValid(i): return False if not isColValid(i): return False for i in range(0, 9, 3): for j in range(0, 9, 3): print(i, j) if not isCelValid(i, j): return False return True sudoku = [[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,8]] if validateSudoku(): print("Sudoku is valid.") else: print("Sudoku is not valid.")
Output:
Sudoku is valid.
You can write a code in any other programming language like C/C++, Java, etc.
Complexity
We are traversing sudoku elements with two for loops. The time complexity of this problem is 2*O(n)
which is equivalent to O(n)
. This is the most optimal solution for solving this problem.
We are not taking any extra space, so the space complexity is O(1)
i.e. constant space.
If you have any doubt or want to suggest any solution for a valid sudoku checker, write me in the comment section.
Comments
James
The code does not take into account that the #s have to be 1-9. For example, each 1 could be subbed with a 0 and the code will still say valid.