Validating a Sudoku Board in Python


 

Validating a Sudoku Board in Python 

Sudoku is a classic puzzle that challenges players to fill a 9×9 grid so that each row, column, and 3×3 subgrid contains all digits from 1 to 9 without repetition. While solving it is fun, validating a given board programmatically is a great exercise in logic and Python skills.

In this post, I’ll walk you through how I built a Python function to validate a Sudoku board. The goal is to check whether the board follows the rules of Sudoku—even if it's not fully solved.

🔍 Problem Statement

Given a 9×9 board filled with digits '1' to '9' and empty cells represented by '.', determine if the board is valid. A valid board must satisfy:

  • Each row contains unique digits.

  • Each column contains unique digits.

  • Each 3×3 subgrid contains unique digits.

🧠 My Solution Strategy

I broke the problem into three parts:

  1. Validate each row.

  2. Validate each column.

  3. Validate each 3×3 subgrid.

Here’s the complete code:

from typing import List

class Solution:
    def get_submatrix(self, sudoku):
        subgrids = []
        for row_block in range(3):
            for col_block in range(3):
                start_row = row_block * 3
                start_col = col_block * 3
                subgrid = [row[start_col:start_col+3] for row in sudoku[start_row:start_row+3]]
                subgrids.append(subgrid)
        return subgrids

    def isValidSudoku(self, board: List[List[str]]) -> bool:
        row = 0
        validRow = 1
        validColumn = 1
        validMartrix = 1

        # Validating rows
        for val in board:
            row += 1
            row_data = []
            for i, data in enumerate(val):
                if data in row_data and data != ".":
                    validRow = 0
                row_data.append(data)

        # Validating columns
        inverseBoard = [list(row) for row in zip(*board)]
        for val in inverseBoard:
            row += 1
            row_data = []
            for i, data in enumerate(val):
                if data in row_data and data != ".":
                    validColumn = 0
                row_data.append(data)

        # Print each 3x3 subgrid
        subgrids = self.get_submatrix(board)
       
       
        for i, grid in enumerate(subgrids):
            row_data = []
            for row in grid:              
                for subelement in row:                  
                    if subelement in row_data and subelement != ".":
                      validMartrix = 0
                    row_data.append(subelement)
               


        if(validRow==0 or validColumn==0 or validMartrix==0):
           print("valid",validMartrix)
           return False
        else:
            return True


# Sample board
board = [["5", "3", ".", ".", "7", "8", ".", ".", "."],
         ["6", ".", ".", "1", "9", "5", ".", ".", "."],
         ["5", "9", "8", ".", ".", ".", ".", "6", "."],
         ["8", ".", ".", ".", "6", ".", ".", ".", "3"],
         ["4", ".", ".", "8", ".", "3", ".", ".", "1"],
         ["7", ".", ".", ".", "2", ".", ".", ".", "6"],
         [".", "6", ".", ".", ".", ".", "2", "8", "."],
         [".", ".", ".", "4", "1", "9", ".", ".", "5"],
         [".", ".", ".", ".", "8", ".", ".", "7", "9"]]

# Run validation
obj = Solution()
obj.isValidSudoku(board)



Comments