Determine if a 9 x 9
Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
1-9
without repetition.1-9
without repetition.3 x 3
sub-boxes of the grid must contain the digits 1-9
without repetition.Note:
Example 1:
Input: board = [["5","3",".",".","7",".",".",".","."] ,["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"]] Output: true
Example 2:
Input: board = [["8","3",".",".","7",".",".",".","."] ,["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"]] Output: false Explanation: Same as Example 1, except with the 5 in the top left corner being modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
Constraints:
board.length == 9
board[i].length == 9
board[i][j]
is a digit 1-9
or '.'
.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To check if a Sudoku puzzle is valid using the brute force approach, we'll examine all the rules to see if any are broken. We'll methodically go through each row, column, and 3x3 square, checking for duplicates.
Here's how the algorithm would work step-by-step:
def is_valid_sudoku(board):
# Check rows for duplicates
for row in board:
if not is_valid_unit(row):
return False
# Check columns for duplicates
for column_index in range(9):
column = [board[row_index][column_index] for row_index in range(9)]
if not is_valid_unit(column):
return False
# Check 3x3 sub-boxes for duplicates
for box_row in range(3):
for box_column in range(3):
# Extract the 3x3 sub-box
sub_box = [
board[row_index][column_index]
for row_index in range(box_row * 3, box_row * 3 + 3)
for column_index in range(box_column * 3, box_column * 3 + 3)
]
if not is_valid_unit(sub_box):
return False
return True
def is_valid_unit(unit):
seen = set()
# Only check for 1-9.
for number in unit:
if number != '.':
if number in seen:
# Duplicates are not allowed
return False
# Record that we have seen this number
seen.add(number)
return True
The puzzle is valid if each row, column, and 3x3 square contains the digits 1-9 without repetition. Instead of checking each row, column, and square separately, we will check them all at the same time to avoid repeating work.
Here's how the algorithm would work step-by-step:
def isValidSudoku(board):
row_values = [{} for _ in range(9)]
column_values = [{} for _ in range(9)]
box_values = [{} for _ in range(9)]
for row_index in range(9):
for column_index in range(9):
current_number = board[row_index][column_index]
if current_number != '.':
current_number = int(current_number)
# Box index is derived from row and column
box_index = (row_index // 3) * 3 + column_index // 3
# Check row
if current_number in row_values[row_index]:
return False
row_values[row_index][current_number] = 1
# Check column
if current_number in column_values[column_index]:
return False
column_values[column_index][current_number] = 1
# Check box
if current_number in box_values[box_index]:
return False
box_values[box_index][current_number] = 1
return True
Case | How to Handle |
---|---|
Null or empty board | Return true immediately as an empty board is considered trivially valid. |
Board with non-digit characters (other than '.') | Check if each character is either a digit '1'-'9' or '.' and return false if not. |
Board with digits outside the range '1'-'9' | Explicitly check that the digits are within the valid range '1'-'9' if not '.'. |
Repetition of digits in a row | Use a set or similar data structure to track digits in each row and return false if a duplicate is found. |
Repetition of digits in a column | Use a set or similar data structure to track digits in each column and return false if a duplicate is found. |
Repetition of digits in a 3x3 sub-box | Use a set or similar data structure to track digits in each 3x3 sub-box and return false if a duplicate is found. |
Board that is not 9x9 | Verify that the board is 9x9 and return false if it's not. |
Input contains leading zeros | While technically a character, treat the presence of '0' similarly to other invalid chars and return false. |