DEV Community

Giuseppe
Giuseppe

Posted on

LeetCode #36. Valid Sudoku

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;
    }
}
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Outer loops: Box selection

for (int boxRow = 0; boxRow < 3; boxRow++) {
    for (int boxCol = 0; boxCol < 3; boxCol++) {
Enter fullscreen mode Exit fullscreen mode

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++) {
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)