Valid Sudoku

Learn to validate a 9x9 Sudoku from LeetCode 36 by checking rows, columns, and 3x3 boxes with HashSet. Clear and efficient guide with complexity analysis and practical approach.

sábado, 16 de agosto de 2025 • 3 min read • Q2BSTUDIO Team

Artificial-Intelligence-

LeetCode #36 Valid Sudoku Rewritten and Translated

Solution Summary The solution checks that a 9 by 9 Sudoku board is valid by verifying rows, columns, and the nine 3 by 3 boxes. For each row, column, and box, a HashSet is used to avoid duplicate digits from 1 to 9, and empty cells represented by the dot character are ignored.

Row Verification The nine rows are traversed with an outer loop. For each row, a new HashSet is created, and the nine cells in that row are traversed. If the cell is not a dot and the attempt to add the digit to the set fails, it means there is a duplicate and the board is not valid.

Column Verification Similarly to rows, the nine columns are traversed with an outer loop. For each column, a new HashSet is created, and the cells in that column are traversed. If a repeated digit appears, the method returns false.

Verification of 3 by 3 Boxes This is the most delicate part because box coordinates must be mapped to board positions. Boxes can be identified by two indices, boxRow and boxCol, which take values 0, 1, and 2, forming the nine combinations 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 Loop for Box Selection Two loops are used: for int boxRow = 0; boxRow < 3; boxRow++ and for int boxCol = 0; boxCol < 3; boxCol++. boxRow indicates the row of boxes and boxCol the column of boxes, thus generating the nine boxes.

Converting Box Coordinates to Board Coordinates To traverse the positions within a box, nested loops are used: for int i = boxRow * 3; i < boxRow * 3 + 3; i++ and for int j = boxCol * 3; j < boxCol * 3 + 3; j++. The key formula is to multiply boxRow and boxCol by 3 to obtain the starting index of the 3 by 3 submatrix.

Example Top-Left Box Box 0,0 boxRow 0 and boxCol 0 produce i 0,1,2 and j 0,1,2. Positions [0,0] [0,1] [0,2] [1,0] [1,1] [1,2] [2,0] [2,1] [2,2] are checked.

Example Center Box Box 1,1 boxRow 1 and boxCol 1 produce i 3,4,5 and j 3,4,5. Positions [3,3] [3,4] [3,5] [4,3] [4,4] [4,5] [5,3] [5,4] [5,5] are checked.

Example Bottom-Right Box Box 2,2 boxRow 2 and boxCol 2 produce i 6,7,8 and j 6,7,8. Positions [6,6] [6,7] [6,8] [7,6] [7,7] [7,8] [8,6] [8,7] [8,8] are checked.

Validation Within Each Box For each box, a new HashSet is created, and the digits found are added. If a digit already exists in the HashSet, validation fails, and the function returns false. This same pattern is applied to rows and columns.

Complexity and Practical Notes The algorithm traverses at most the 81 cells of the board a constant number of times per row, column, and box, so its cost is linear in the size of the board and, in practice, constant for a standard Sudoku. It is simple, clear, and efficient for validating initial Sudoku boards.

About Q2BSTUDIO Q2BSTUDIO is a software development company specialized in custom applications and custom software for businesses of all sizes. We offer advanced services in artificial intelligence and AI for businesses, AI agent development, cybersecurity, and consulting in AWS and Azure cloud services. We also provide business intelligence services and visualization with Power BI to turn data into actionable decisions. Our projects combine expertise in artificial intelligence, cybersecurity, and cloud architecture to deliver secure and scalable custom applications.

Keywords and Positioning custom applications custom software artificial intelligence cybersecurity AWS and Azure cloud services business intelligence services AI for businesses AI agents Power BI

Contact and Call to Action If you want to develop a custom solution, improve the security of your systems, implement artificial intelligence in your processes, or migrate to the cloud with AWS and Azure cloud services, contact Q2BSTUDIO and let us transform your ideas into high-value products.

OUR SERVICES

How we can help you

Do you have a project in mind?

Tell us your vision and we'll turn it into a software solution. Whatever the scope, we make your idea real.