This content originally appeared on DEV Community and was authored by Giuseppe
class Solution {
public boolean isValidSudoku(char[][] board) {
// Check all rows
for (int i = 0; i < 9; i++) {
Set<Character> rowSet = new HashSet<>();
for (int j = 0; j < 9; j++) {
char cell = board[i][j];
if (cell != '.' && !rowSet.add(cell)) {
return false;
}
}
}
// Check all columns
for (int j = 0; j < 9; j++) {
Set<Character> colSet = new HashSet<>();
for (int i = 0; i < 9; i++) {
char cell = board[i][j];
if (cell != '.' && !colSet.add(cell)) {
return false;
}
}
}
// Check all 3x3 boxes
for (int boxRow = 0; boxRow < 3; boxRow++) {
for (int boxCol = 0; boxCol < 3; boxCol++) {
Set<Character> boxSet = new HashSet<>();
for (int i = boxRow * 3; i < boxRow * 3 + 3; i++) {
for (int j = boxCol * 3; j < boxCol * 3 + 3; j++) {
char cell = board[i][j];
if (cell != '.' && !boxSet.add(cell)) {
return false;
}
}
}
}
}
return true;
}
}
the last block of code checks if all nine 3×3 boxes in the Sudoku board are valid. It's the most complex part because it needs to map from "box coordinates" to actual board positions.
Understanding the box structure:
A 9×9 Sudoku has nine 3×3 boxes arranged like this:
Box(0,0) | Box(0,1) | Box(0,2)
Box(1,0) | Box(1,1) | Box(1,2)
Box(2,0) | Box(2,1) | Box(2,2)
Outer loops: Box selection
for (int boxRow = 0; boxRow < 3; boxRow++) {
for (int boxCol = 0; boxCol < 3; boxCol++) {
boxRow goes 0, 1, 2 (which row of boxes)
boxCol goes 0, 1, 2 (which column of boxes)
This gives us all 9 box combinations: (0,0), (0,1), (0,2), (1,0), etc.
Converting box coordinates to board coordinates:
for (int i = boxRow * 3; i < boxRow * 3 + 3; i++) {
for (int j = boxCol * 3; j < boxCol * 3 + 3; j++) {
The key formula: boxRow * 3 and boxCol * 3
Let's trace through examples:
Box (0,0) - Top-left box:
boxRow = 0, boxCol = 0
i goes from 0*3 = 0 to 0*3+3 = 3 (exclusive) → i = 0,1,2
j goes from 0*3 = 0 to 0*3+3 = 3 (exclusive) → j = 0,1,2
Checks board positions: [0,0], [0,1], [0,2], [1,0], [1,1], [1,2], [2,0], [2,1], [2,2]
Box (1,1) - Center box:
boxRow = 1, boxCol = 1
i goes from 1*3 = 3 to 1*3+3 = 6 (exclusive) → i = 3,4,5
j goes from 1*3 = 3 to 1*3+3 = 6 (exclusive) → j = 3,4,5
Checks board positions: [3,3], [3,4], [3,5], [4,3], [4,4], [4,5], [5,3], [5,4], [5,5]
Box (2,2) - Bottom-right box:
boxRow = 2, boxCol = 2
i goes from 2*3 = 6 to 2*3+3 = 9 (exclusive) → i = 6,7,8
j goes from 2*3 = 6 to 2*3+3 = 9 (exclusive) → j = 6,7,8
Checks board positions: [6,6], [6,7], [6,8], [7,6], [7,7], [7,8], [8,6], [8,7], [8,8]
The validation:
For each 3×3 box, it creates a fresh HashSet and ensures no digit 1-9 appears twice within that box, just like the row and column checks.
This content originally appeared on DEV Community and was authored by Giuseppe

Giuseppe | Sciencx (2025-08-07T13:21:12+00:00) LeetCode #36. Valid Sudoku. Retrieved from https://www.scien.cx/2025/08/07/leetcode-36-valid-sudoku/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.