Alexander Obregon's Substack

Java LeetCode Solutions

LeetCode #36: Valid Sudoku — Solved in Java

Basic Scans and Bit Mask Validation

Alexander Obregon's avatar
Alexander Obregon
Nov 29, 2025
∙ Paid
LeetCode Logo
Image Source

Valid Sudoku checks a nine by nine grid filled with digits and dots to confirm that no row, column, or three by three box repeats a digit. Each region needs to stay free of duplicates, so the problem turns into a scan across the board to make sure every filled cell plays by those rules. The return is a single boolean value, which keeps the goal focused on verifying the current state of the board rather than building or searching for anything new.

Solving this in Java benefits from thinking about how to sweep across structured data while keeping small bits of state tied to each region. A row, column, or box only needs a short record of which digits have appeared, and that opens a path to either boolean flags or a compact bit mask. These two styles guide the process of reading the grid in a steady flow and letting small checks reveal whether a region stays valid, which fits nicely with the kind of reasoning interviewers look for when a board must be validated with simple, repeatable logic.

LeetCode: Valid Sudoku

Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

  1. Each row must contain the digits 1-9 without repetition.

  2. Each column must contain the digits 1-9 without repetition.

  3. Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.

Note:

  • A Sudoku board (partially filled) could be valid but is not necessarily solvable.

  • Only the filled cells need to be validated according to the mentioned rules.

Example 1:

Input: board = 
[[”5”,”3”,”.”,”.”,”7”,”.”,”.”,”.”,”.”]
,[”6”,”.”,”.”,”1”,”9”,”5”,”.”,”.”,”.”]
,[”.”,”9”,”8”,”.”,”.”,”.”,”.”,”6”,”.”]
,[”8”,”.”,”.”,”.”,”6”,”.”,”.”,”.”,”3”]
,[”4”,”.”,”.”,”8”,”.”,”3”,”.”,”.”,”1”]
,[”7”,”.”,”.”,”.”,”2”,”.”,”.”,”.”,”6”]
,[”.”,”6”,”.”,”.”,”.”,”.”,”2”,”8”,”.”]
,[”.”,”.”,”.”,”4”,”1”,”9”,”.”,”.”,”5”]
,[”.”,”.”,”.”,”.”,”8”,”.”,”.”,”7”,”9”]]
Output: true

Example 2:

Input: board = 
[[”8”,”3”,”.”,”.”,”7”,”.”,”.”,”.”,”.”]
,[”6”,”.”,”.”,”1”,”9”,”5”,”.”,”.”,”.”]
,[”.”,”9”,”8”,”.”,”.”,”.”,”.”,”6”,”.”]
,[”8”,”.”,”.”,”.”,”6”,”.”,”.”,”.”,”3”]
,[”4”,”.”,”.”,”8”,”.”,”3”,”.”,”.”,”1”]
,[”7”,”.”,”.”,”.”,”2”,”.”,”.”,”.”,”6”]
,[”.”,”6”,”.”,”.”,”.”,”.”,”2”,”8”,”.”]
,[”.”,”.”,”.”,”4”,”1”,”9”,”.”,”.”,”5”]
,[”.”,”.”,”.”,”.”,”8”,”.”,”.”,”7”,”9”]]
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.

Constraints:

  • board.length == 9

  • board[i].length == 9

  • board[i][j] is a digit 1-9 or ‘.’.

I usually include the full code as a screenshot to make it easier to read on phones since Substack’s code formatting can be tough to follow on smaller screens. Longer solutions tend to be much longer than usual though, so capturing everything in one image isn’t always practical. You can still copy and run the complete solutions below, and at the end of the article you’ll find the same full code again with more detailed comments to help follow along.

Solution 1: Basic Row, Column, and Box Scans

This solution keeps everything direct by scanning rows, columns, and the 3×3 boxes separately. A small boolean array tracks which digits have appeared. The moment a repeated digit shows up inside the same row, column, or box, the method returns false.

Keep reading with a 7-day free trial

Subscribe to Alexander Obregon's Substack to keep reading this post and get 7 days of free access to the full post archives.

Already a paid subscriber? Sign in
© 2025 Alexander Obregon · Privacy ∙ Terms ∙ Collection notice
Start your SubstackGet the app
Substack is the home for great culture