Last Updated: November 13, 2025
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:
Input: board
Output: true
Input: board
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.
board.length == 9board[i].length == 9board[i][j] is a digit 1-9 or '.'.The Brute Force approach involves checking all rows, columns, and sub-grids (3x3) to ensure they contain unique numbers. This is straightforward but can be inefficient due to repeated checks.
To optimize, use a HashSet for keeping track of seen numbers efficiently. We iterate once through the board, and at each step, we ensure that no number appears twice in its row, column or sub-grid.