By Long Luo

# 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 hash table 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 hash table for counting.

We create a two-dimensional array rows and columns to record the number of occurrences of each number in each row and column of Sudoku, and create a three-dimensional 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. 😉😃💗