Check whether a partially filled Sudoku board satisfies all Sudoku rules.
You are given a partially filled Sudoku board. Some cells contain digits from 1 to 9, and empty cells are marked with '.'.
Determine whether the current board configuration is valid.
A valid Sudoku board must satisfy all of the following rules:
- Each row contains no duplicate digits from
1to9. - Each column contains no duplicate digits from
1to9. - Each of the nine sub-boxes contains no duplicate digits from
1to9.
Only the filled cells need to be checked; the board does not need to be solvable.
Input Format
- A grid of characters.
- Each cell is either a digit from
1to9or'.'for empty.
Output Format
- Return
trueif the board is valid according to Sudoku rules. - Otherwise, return
false.
Constraints
- The board size is fixed at .
- Empty cells marked
'.'should be ignored. - Digits are limited to
1through9.
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
Explanation
No row, column, or 3x3 sub-box contains duplicate filled digits.
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
The digit 8 appears twice in the first column, so the board is invalid.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.