36. Valid Sudok #2114
-
Topics: Determine if a
Note:
Example 1:
Example 2:
Constraints:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to determine if a given 9x9 Sudoku board is valid according to the standard Sudoku rules. The rules require that each row, each column, and each of the nine 3x3 sub-boxes contains unique digits from 1 to 9, ignoring any empty cells (represented by '.'). ApproachThe approach involves checking three main conditions:
To efficiently check these conditions, we use arrays to keep track of digits encountered in each row, column, and sub-box. For each non-empty cell in the board, we check if the digit has already been seen in the current row, column, or sub-box. If it has, the board is invalid. Otherwise, we mark the digit as seen in the respective row, column, and sub-box. The key insight is to compute the index of the sub-box for any given cell. The sub-box index can be calculated using the formula: Let's implement this solution in PHP: 36. Valid Sudok <?php
/**
* @param String[][] $board
* @return Boolean
*/
function isValidSudoku($board) {
$rows = array_fill(0, 9, array());
$columns = array_fill(0, 9, array());
$boxes = array_fill(0, 9, array());
for ($i = 0; $i < 9; $i++) {
for ($j = 0; $j < 9; $j++) {
$num = $board[$i][$j];
if ($num == '.') {
continue;
}
$boxIndex = (int)($i / 3) * 3 + (int)($j / 3);
if (isset($rows[$i][$num]) || isset($columns[$j][$num]) || isset($boxes[$boxIndex][$num])) {
return false;
}
$rows[$i][$num] = true;
$columns[$j][$num] = true;
$boxes[$boxIndex][$num] = true;
}
}
return true;
}
// Test cases
// --------------------
// Example 1: Valid board
// --------------------
$board1 = array(
array("5","3",".",".","7",".",".",".","."),
array("6",".",".","1","9","5",".",".","."),
array(".","9","8",".",".",".",".","6","."),
array("8",".",".",".","6",".",".",".","3"),
array("4",".",".","8",".","3",".",".","1"),
array("7",".",".",".","2",".",".",".","6"),
array(".","6",".",".",".",".","2","8","."),
array(".",".",".","4","1","9",".",".","5"),
array(".",".",".",".","8",".",".","7","9")
);
echo "Example 1: " . (isValidSudoku($board1) ? "true" : "false") . "\n";
// --------------------
// Example 2: Invalid board
// --------------------
$board2 = array(
array("8","3",".",".","7",".",".",".","."),
array("6",".",".","1","9","5",".",".","."),
array(".","9","8",".",".",".",".","6","."),
array("8",".",".",".","6",".",".",".","3"),
array("4",".",".","8",".","3",".",".","1"),
array("7",".",".",".","2",".",".",".","6"),
array(".","6",".",".",".",".","2","8","."),
array(".",".",".","4","1","9",".",".","5"),
array(".",".",".",".","8",".",".","7","9")
);
echo "Example 2: " . (isValidSudoku($board2) ? "true" : "false") . "\n";
?> Explanation:
This approach efficiently checks the validity of the Sudoku board by leveraging arrays to track seen digits, ensuring optimal performance with a time complexity of O(1) per cell check, leading to an overall O(81) or constant time complexity. |
Beta Was this translation helpful? Give feedback.
We need to determine if a given 9x9 Sudoku board is valid according to the standard Sudoku rules. The rules require that each row, each column, and each of the nine 3x3 sub-boxes contains unique digits from 1 to 9, ignoring any empty cells (represented by '.').
Approach
The approach involves checking three main conditions:
To efficiently check these conditions, we use arrays to keep track of digits encountered in each row, column, and sub-box. F…