Valid Sudoku
Valid Sudoku is LeetCode 36 (Medium). You get a 9 × 9 Sudoku board where each cell is a digit 1–9 or . for empty. Return true if the filled cells break none of the three rules:
- no digit twice in any row,
- no digit twice in any column,
- no digit twice in any of the nine 3 × 3 boxes.
Only the digits already on the board are checked. The board does not have to be full, and it does not have to be solvable.
- board.length == 9
- board[i].length == 9
- board[i][j] is a digit 1-9 or '.'.
Intuition
Every filled cell belongs to exactly three groups: its row, its column and its box. Give each of the 27 groups a set of the digits seen in it so far. Then one pass over the board is enough: for each digit, ask its three sets whether they already hold it. A yes is a repeat, so the board is invalid. A no means the digit is new in all three groups, so record it in all three and move on.
The only real work is naming the box. Rows 0–2 form band 0, rows 3–5 band 1, rows 6–8 band 2, and columns split the same way. So the box number is (r // 3) * 3 + c // 3, which counts the boxes 0 to 8 left to right, top to bottom.
Checking that no value repeats inside several overlapping groups is a job for one seen-set per group, all filled in a single scan. Sudoku Solver keeps the same 27 sets but uses them to decide which digits may go in an empty cell.
Approach
Before reading on, work out which box the cell in row 4, column 7 belongs to, and write a formula that gives that number for any cell. Aim for one pass over the 81 cells.
Make one set per row, column and box
Create nine sets for rows, nine for columns and nine for boxes. Each set will hold the digits found so far in that group, so asking "has this group seen a 5?" is a single lookup.
Skip empty cells
Walk the board row by row. A . holds no digit, so it cannot repeat anything; move straight to the next cell.
Work out the box number
For a digit at row r and column c, compute b = (r // 3) * 3 + c // 3. The first part picks the band of three box rows and the second picks the box inside that band, so the nine boxes get the numbers 0 to 8.
Stop at the first repeat
If the digit is already in rows[r], cols[c] or boxes[b], return false. One repeat breaks a rule, so nothing later on the board can make it valid again.
Otherwise record the digit three times
Add the digit to all three sets, so any later copy in the same row, column or box is caught. If the scan reaches the end with no repeat, return true.
Code in Python, C++ and Java
Common pitfalls
Adding the band numbers instead of combining them
b = r // 3 + c // 3
b = (r // 3) * 3 + c // 3
The sum only runs from 0 to 4, so different boxes share a number. Cells (0, 3) and (3, 0) both get box 1, and a 7 in each makes the board look invalid although the two 7s sit in different boxes.
Answering whether the board can be solved
run a Sudoku solver and return whether it succeeds
only check the digits already on the board for repeats
A board with no repeats can still have no solution, and this problem still wants true for it. Solving is also far slower than one scan of 81 cells.
Edge cases
Nothing is ever added to a set, so no repeat is possible and the answer is true.
Complexity
Valid Sudoku FAQ
Can the 27 sets be replaced by something smaller?
Yes. Give each row, column and box one integer and use bit d for digit d. Testing is mask & (1 << d) and recording is mask |= 1 << d, which uses 27 integers in place of 27 sets. It is the same algorithm, only a more compact store.
Why do the C++ and Java versions use boolean arrays instead of sets?
The digits are only 1 to 9, so rows[r][d] with d = digit - 1 is a direct yes/no lookup. It does the same job as a set without hashing.