Problem 10 of 198MediumLeetCode~30 min

Valid Sudoku

Hash Tables (Frequency Counter)

Asked at 12 companies

7 recent

  • Apple
  • Amazon
  • Bloomberg
  • Google
  • Microsoft
  • Meta
  • tcs
  • Confluent
  • Samsara
  • Wissen Technology
+2 more
  • EarnIn
  • Snap

Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.

  1. 1Basics
  2. 2Paper
  3. 3Code it
  4. 4Watch
  5. 5Visualize
  6. 6Accepted
  7. 7Review

Go step by step: try it yourself before the video and the visual. Progress saves on this device.

0 / 130 pointsSolved (Medium) 0/40Reviews 0/50Mastered 0/25Practice steps 0/15

Step 1

Know these before you start

Practise each one here, or tick what you already know

Concept video
Learn ›Practise ✎Learn ›Practise ✎Learn ›Practise ✎Learn ›Practise ✎

Builds on: Counting subarrays with a map of totals

Step 2

Try it on paper

5–10 minutes, no code yet

In the wild · Caches and duplicate detection: A hash map answers “have I seen this before?” in one step. It's how apps cache results, and how a photo app spots the same picture uploaded twice.

About this problem

Multiple hash sets for row/col/box validation

Try it in the editor below for 30 min: passing its tests counts as solved and starts your reviews. The full statement is on LeetCode.

  1. Example 1

    Input
    board = a 9×9 grid, partly filled (the classic sample board)
    Output
    true
    Walk through it step by step(32 steps · shows the approach, so try first)
    1. A board is valid if no digit repeats in any row, column, or 3×3 box. We keep one set per row, per column, and per box, and check each filled cell against them.
    2. 5 at row 0, column 0: not yet in row 0, column 0, or box 0, so it's fine. Remember it in all three.
    3. 3 at row 0, column 1: not yet in row 0, column 1, or box 0, so it's fine. Remember it in all three.
    4. 7 at row 0, column 4: not yet in row 0, column 4, or box 1, so it's fine. Remember it in all three.
    5. 6 at row 1, column 0: not yet in row 1, column 0, or box 0, so it's fine. Remember it in all three.
    6. 1 at row 1, column 3: not yet in row 1, column 3, or box 1, so it's fine. Remember it in all three.
    7. 9 at row 1, column 4: not yet in row 1, column 4, or box 1, so it's fine. Remember it in all three.
    8. 5 at row 1, column 5: not yet in row 1, column 5, or box 1, so it's fine. Remember it in all three.
    9. 9 at row 2, column 1: not yet in row 2, column 1, or box 0, so it's fine. Remember it in all three.
    10. 8 at row 2, column 2: not yet in row 2, column 2, or box 0, so it's fine. Remember it in all three.
    11. 6 at row 2, column 7: not yet in row 2, column 7, or box 2, so it's fine. Remember it in all three.
    12. 8 at row 3, column 0: not yet in row 3, column 0, or box 3, so it's fine. Remember it in all three.
    13. 6 at row 3, column 4: not yet in row 3, column 4, or box 4, so it's fine. Remember it in all three.
    14. 3 at row 3, column 8: not yet in row 3, column 8, or box 5, so it's fine. Remember it in all three.
    15. 4 at row 4, column 0: not yet in row 4, column 0, or box 3, so it's fine. Remember it in all three.
    16. 8 at row 4, column 3: not yet in row 4, column 3, or box 4, so it's fine. Remember it in all three.
    17. 3 at row 4, column 5: not yet in row 4, column 5, or box 4, so it's fine. Remember it in all three.
    18. 1 at row 4, column 8: not yet in row 4, column 8, or box 5, so it's fine. Remember it in all three.
    19. 7 at row 5, column 0: not yet in row 5, column 0, or box 3, so it's fine. Remember it in all three.
    20. 2 at row 5, column 4: not yet in row 5, column 4, or box 4, so it's fine. Remember it in all three.
    21. 6 at row 5, column 8: not yet in row 5, column 8, or box 5, so it's fine. Remember it in all three.
    22. 6 at row 6, column 1: not yet in row 6, column 1, or box 6, so it's fine. Remember it in all three.
    23. 2 at row 6, column 6: not yet in row 6, column 6, or box 8, so it's fine. Remember it in all three.
    24. 8 at row 6, column 7: not yet in row 6, column 7, or box 8, so it's fine. Remember it in all three.
    25. 4 at row 7, column 3: not yet in row 7, column 3, or box 7, so it's fine. Remember it in all three.
    26. 1 at row 7, column 4: not yet in row 7, column 4, or box 7, so it's fine. Remember it in all three.
    27. 9 at row 7, column 5: not yet in row 7, column 5, or box 7, so it's fine. Remember it in all three.
    28. 5 at row 7, column 8: not yet in row 7, column 8, or box 8, so it's fine. Remember it in all three.
    29. 8 at row 8, column 4: not yet in row 8, column 4, or box 7, so it's fine. Remember it in all three.
    30. 7 at row 8, column 7: not yet in row 8, column 7, or box 8, so it's fine. Remember it in all three.
    31. 9 at row 8, column 8: not yet in row 8, column 8, or box 8, so it's fine. Remember it in all three.
    32. Every filled cell passed all three checks, so the board is valid.

    ▶ Watch these steps animate

Code it here

Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor

Step 6

Get it accepted

All test cases pass

Step 7

Review it so it sticks

Re-solve from memory after

The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.

Your notes

Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.

Solve it yourself before you look

Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.

Hidden: optimal solution

Stuck? One hint at a time.

    Srujan's notes aren't here yet. When he solves this one in his journal, his own writeup and solutions show up on this page too.