By Long Luo

Here shows 2 Approaches to slove this problem: HashSet and Array.

# HashSet

We can use a HashSet to record the number of occurrences of each number in each row, each column and each sub-box.

Traverse the Sudoku once, update the count in the HashMap during the traversal process, and determine whether the Sudoku board could be valid.

This is version 1.0 code.

In fact, we can only traversal once. The index of each sub-box is $3 \times (i / 3) + j / 3$, so we can write better code.

## Analysis

• Time Complexity: $O(1)$.
• Space Complexity: $O(1)$.

# Array

Since numbers in Sudoku range from $1$ to $9$, we can use array instead of the HashMap for counting.

We create a 2D Array, the rows and columns which record the number of occurrences of each number in each row and column of Sudoku, and create a 3D Array subboxes to record the number of occurrences of each number in each sub-box.

If the count is greater than $1$, the Sudoku is not valid.

## Analysis

• Time Complexity: $O(1)$.
• Space Complexity: $O(1)$.

All suggestions are welcome.
If you have any query or suggestion please comment below.
Please upvote👍 if you like💗 it. Thank you:-)

Explore More Leetcode Solutions. 😉😃💗