Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Valid Sudoku in Python
A valid Sudoku puzzle must follow three key rules for all filled cells: no duplicate digits in any row, column, or 3x3 sub-box. We need to validate only the filled cells, not solve the entire puzzle.
Validation Rules
A 9x9 Sudoku board is valid when ?
- Each row contains digits 1-9 without repetition
- Each column contains digits 1-9 without repetition
- Each of the 9 (3x3) sub-boxes contains digits 1-9 without repetition
Algorithm Approach
We'll use three dictionaries to track seen digits in rows, columns, and 3x3 blocks. For each cell, we calculate which 3x3 block it belongs to using integer division.
Example
Let's validate this Sudoku board ?
class Solution:
def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
for i in range(9):
row = {}
column = {}
block = {}
row_cube = 3 * (i // 3)
column_cube = 3 * (i % 3)
for j in range(9):
# Check row
if board[i][j] != '.' and board[i][j] in row:
return False
row[board[i][j]] = 1
# Check column
if board[j][i] != '.' and board[j][i] in column:
return False
column[board[j][i]] = 1
# Check 3x3 block
rc = row_cube + j // 3
cc = column_cube + j % 3
if board[rc][cc] in block and board[rc][cc] != '.':
return False
block[board[rc][cc]] = 1
return True
# Test with a valid Sudoku board
solution = Solution()
board = [
["5","3",".",".",".",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
result = solution.isValidSudoku(board)
print("Is valid Sudoku:", result)
Is valid Sudoku: True
How the Algorithm Works
The algorithm processes each position systematically ?
-
Row validation: Check
board[i][j]for duplicates in rowi -
Column validation: Check
board[j][i]for duplicates in columni -
Block validation: Calculate block position using
row_cube + j//3andcolumn_cube + j%3
Testing with Invalid Board
Let's test with an invalid board that has duplicate '8' in the first row ?
# Invalid board - duplicate '8' in first row
invalid_board = [
["8","3",".",".",".",".",".",".","."],
["8",".",".","1","9","5",".",".","."], # Another '8' in same row
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
result = solution.isValidSudoku(invalid_board)
print("Is valid Sudoku:", result)
Is valid Sudoku: False
Conclusion
This algorithm efficiently validates Sudoku boards in O(1) space and time complexity since the board size is fixed at 9x9. The key insight is simultaneously checking rows, columns, and 3x3 blocks in a single pass through the grid.
