Valid Sudoku

MediumMatrix · Hashing · Bitmask🧩 Matrix TraversalLeetCode #36 ↗Updated

Problem

You are given a partly filled 9 x 9 Sudoku grid of chars, where '.' marks an empty cell. Decide whether the digits placed so far break any rule: no digit 1-9 may appear twice in the same row, the same column, or the same 3 x 3 box. You do not need to check whether the puzzle can actually be solved.
Inputa standard puzzle with 30 clues and no repeatsOutputtrue
Inputsame grid but the top-left cell changed from '5' to '8' (column 0 already has an 8)Outputfalse

board is 9 x 9  ·  each cell is a digit '1'-'9' or '.'

Try it yourself

Java
class Solution {
    public boolean isValidSudoku(char[][] board) {
        
    }
}
Your code is saved in this browser.Submit on LeetCode ↗
Key insight
Every filled cell belongs to exactly three units: row r, column c and box (r/3)*3 + c/3. Keep a “seen” record per unit and the whole check becomes one pass over the 81 cells.
PatternMatrix Traversal

Instinct A 2D grid you must walk in a fixed geometric order (spiral, diagonal, layer, rotate) or update in place - think boundaries, direction vectors and index maths.

Memory hack Walk the walls, then shrink the room - four fences (top, bottom, left, right) close in after every lap.

Open the Matrix Traversal guide →

Approaches

Brute ForceRescan Row, Column and Box per Cell

Time O(N³)Space O(1)

For each filled cell, walk its row, its column and its box and see whether any other cell holds the same digit. Any hit means the board is invalid. With N = 9 that is 81 cells × ~27 comparisons.

Java
public boolean isValidSudoku(char[][] board) {
    for (int r = 0; r < 9; r++) {
        for (int c = 0; c < 9; c++) {
            char d = board[r][c];
            if (d == '.') continue;
            for (int k = 0; k < 9; k++) {
                if (k != c && board[r][k] == d) return false;   // same row
                if (k != r && board[k][c] == d) return false;   // same column
            }
            int br = r / 3 * 3, bc = c / 3 * 3;
            for (int i = br; i < br + 3; i++) {
                for (int j = bc; j < bc + 3; j++) {
                    if ((i != r || j != c) && board[i][j] == d) return false; // same box
                }
            }
        }
    }
    return true;
}

No extra memory, but repeats the same comparisons many times.

At a glance

LevelApproachTimeSpace
Brute ForceRescan Row, Column and Box per CellO(N³)O(1)
BetterOne Pass with Hash SetsO(N²)O(N²)
OptimalOne Pass with BitmasksO(N²)O(N)
Common pitfalls
  • Getting the box index wrong, e.g. r / 3 + c / 3 (collides boxes).
  • Trying to solve the puzzle — only the current placements must be checked.
  • Forgetting to skip '.' cells, which then look like duplicates of each other.
Interview tips
  • Clarify: valid ≠ solvable.
  • Point out that with a fixed 9 x 9 board everything is technically O(1); state it in N too.
  • Offer the bitmask version as the memory-lean refinement of the hash sets.
Pattern
Per-group seen sets → detect duplicates within overlapping groups (rows, columns, blocks) in one scan.
My notes saved in this browser