Valid Sudoku Checker in Python

Shape Image One

Valid Sudoku Checker in Python

Problem statement: You have given sudoku as a matrix. For example,

sudoku validity checker 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],

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, the set() 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],
if validateSudoku():
    print("Sudoku is valid.")
    print("Sudoku is not valid.")


Sudoku is valid.

You can write a code in any other programming language like C/C++, Java, etc.


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.

1 Comment

  1. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *