#36

Valid Sudoku

Medium Row/column/box sets

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Check whether a partially filled 9×9 Sudoku board breaks any rule: no digit repeats in a row, a column or a 3×3 box. The puzzle doesn't need to be solvable.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: does any row, column or 3×3 box repeat a digit?

Why this works: remember which digits each row, column and box has seen. Box number = (r/3)·3 + c/3.

Example: a '5' at (0,0) and another '5' at (1,1)

both in box 0 → repeat → false

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Computing the box index as r / 3 + c / 3, which maps different boxes to the same index.

Solve on LeetCode ↗
Complexity
  • Time: O(81) = O(1).
  • Space: O(1).
Topic: 2D Arrays & Matrix
🎬 Episode 14: video coming soon