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 == 9board[i].length == 9board[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 board is valid using a brute force approach, you can systematically inspect every single row, column, and smaller region to ensure no numbers are repeated. This exhaustive checking guarantees that every rule of the game is respected across the entire board.
Here's how the algorithm would work step-by-step:
def isValidSudoku(board):
# We must check every row independently to ensure no numbers from one to nine repeat
for row_index in range(9):
seen_digits = set()
for col_index in range(9):
current_value = board[row_index][col_index]
if current_value != ".":
if current_value in seen_digits:
return False
seen_digits.add(current_value)
# We check each column separately since vertical duplicates violate sudoku rules
for col_index in range(9):
seen_digits = set()
for row_index in range(9):
current_value = board[row_index][col_index]
if current_value != ".":
if current_value in seen_digits:
return False
seen_digits.add(current_value)
# Each three by three subgrid must be validated to catch local duplicates
for box_row in range(0, 9, 3):
for box_col in range(0, 9, 3):
seen_digits = set()
for row_offset in range(3):
for col_offset in range(3):
current_row = box_row + row_offset
current_col = box_col + col_offset
current_value = board[current_row][current_col]
if current_value != ".":
if current_value in seen_digits:
return False
seen_digits.add(current_value)
return TrueTo efficiently check if a Sudoku board is valid, we can inspect every single cell once while keeping track of the numbers we have already seen. By using three distinct mental checklists for every row, column, and smaller box, we can instantly spot if any number is repeated illegally.
Here's how the algorithm would work step-by-step:
def isValidSudoku(board):
seen_elements_set = set()
# We traverse every single cell in the 9x9 grid to inspect each number.
for row_index in range(9):
for col_index in range(9):
current_element = board[row_index][col_index]
if current_element == ".":
continue
box_index = (row_index // 3, col_index // 3)
row_identifier = (row_index, current_element)
col_identifier = (current_element, col_index)
box_identifier = (box_index, current_element)
# We check the three distinct memory logs to guarantee absolute uniqueness.
if row_identifier in seen_elements_set or col_identifier in seen_elements_set or box_identifier in seen_elements_set:
return False
seen_elements_set.add(row_identifier)
seen_elements_set.add(col_identifier)
seen_elements_set.add(box_identifier)
# Reaching this point guarantees the entire board contains no rule violations.
return True| Case | How to Handle |
|---|---|
| Board contains only empty cells denoted by period characters | The validation loop skips non-digit characters and immediately returns true for an entirely empty board |
| Board has identical digits appearing multiple times in the same row, column, or 3x3 sub-box | The tracking sets detect the duplicate entry upon insertion and immediately return false |
| Input board contains invalid characters outside of digits 1 through 9 and period characters | The validation logic ignores or fails on unexpected characters, so input sanitization or strict digit checks should be applied beforehand |
| Maximum-sized board filled completely with a valid Sudoku configuration | The algorithm iterates through all 81 cells once in O(1) time and space complexity due to the fixed 9x9 dimensions |
| Board with invalid values localized strictly to the final 3x3 sub-box at the bottom right | The single-pass traversal efficiently evaluates all rows, columns, and boxes, catching the error regardless of its position |
| Inputs with multiple duplicate values across different rows sharing the same column or sub-box | String-formatted identifiers for rows, columns, and boxes ensure collisions are accurately tracked across distinct geometric regions |