ExamAdda Logo

Valid Sudoku

Easy

Determine if a 9 × 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 × 3 sub-boxes must contain the digits 1-9 without repetition.

Note:

A Sudoku board may be valid but is not necessarily solvable.

Empty cells are represented by '.'.

Example 1

Input

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

Explanation

Every row, column, and 3 × 3 sub-box satisfies the Sudoku rules.

Example 2

Input

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

The digit '8' appears twice in the first column, violating Sudoku rules.

Constraints

board.length == 9
board[i].length == 9
board[i][j] is a digit '1'-'9' or '.'

Hints:

Hint 1

Check each row, each column, and each 3 × 3 sub-box independently.

Hint 2

Use three hash sets (or boolean arrays) to track digits seen in rows, columns, and sub-boxes. If a digit is encountered twice in the same row, column, or box, the board is invalid.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 6, 2026

Last updated

Aug 7, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output