Valid Sudoku

Asked byAmazonMicrosoftUberCruise AutomationAppleFacebook

Problem

Given a 9x9 Sudoku board, determine whether the currently filled cells satisfy Sudoku's placement rules: each row, each column, and each of the nine 3x3 sub-boxes must contain the digits 1-9 without repetition. Empty cells don't need to be validated — you're only checking that the digits already placed don't conflict.

Examples

Example 1
Input:board = 9x9 grid with a few digits placed, no conflicts
Output:true
Example 2
Input:board with two '8's in the same column
Output:false

Constraints

  • board.length == 9, board[i].length == 9
  • Cells contain digits 1-9 or are empty.

Solve it in the editor. Sign in free to run your Python or JavaScript against test cases, get a verdict, and track your attempts.

Solve on FeatCode →

How to approach it: the Arrays & Hashing pattern

Arrays give O(1) index access; hash maps and sets give O(1) average lookup, insert, and delete by trading space for speed. Together they're the toolkit for turning brute-force O(n²) comparisons into a single O(n) pass.

Look for this pattern when

  • You need to check "have I seen this value before?" quickly.
  • You're counting occurrences or grouping items by a derived key (e.g. sorted letters for anagrams).
  • A nested loop is comparing every pair — a hash map can often replace the inner loop.

Read the full Arrays & Hashing guide →

Video walkthroughs

Original problem on LeetCode ↗

More Arrays & Hashing problems