|
2 | 2 |
|
3 | 3 | ## Intuition
|
4 | 4 |
|
5 |
| -The core idea is to ensure that each number from `1` to `9` appears only once in each row, each column, and each of the |
6 |
| -nine 3x3 sub-boxes of the Sudoku board. By keeping track of the numbers we have seen so far in each row, column, and |
| 5 | +The core idea is to ensure that each number from `1` to `9` appears only once in each row, each column, and each of the |
| 6 | +nine 3x3 sub-boxes of the Sudoku board. By keeping track of the numbers we have seen so far in each row, column, and |
7 | 7 | box, we can efficiently determine if the Sudoku board is valid or not.
|
8 | 8 |
|
9 | 9 | ## Approach
|
10 | 10 |
|
11 | 11 | **Initialization:** We use three hash maps to keep track of the characters we encounter:
|
12 |
| - - `rows` to track characters for each row. |
13 |
| - - `cols` to track characters for each column. |
14 |
| - - `boxes` to track characters for each 3x3 sub-box. |
15 |
| - - Each of these maps contains sets, which will store the characters seen so far. |
| 12 | + |
| 13 | +- `rows` to track characters for each row. |
| 14 | +- `cols` to track characters for each column. |
| 15 | +- `boxes` to track characters for each 3x3 sub-box. |
| 16 | +- Each of these maps contains sets, which will store the characters seen so far. |
16 | 17 |
|
17 | 18 | **Filling the Maps:**
|
18 |
| - - We iterate over each cell in the board using nested loops. |
19 |
| - - For each cell, if the character is not `.`, we do the following: |
| 19 | + |
| 20 | +- We iterate over each cell in the board using nested loops. |
| 21 | +- For each cell, if the character is not `.`, we do the following: |
20 | 22 | - Calculate the corresponding 3x3 box index using integer division (`rb = i / 3` and `cb = j / 3`).
|
21 | 23 | - Check if the character already exists in the respective row, column, or box set.
|
22 | 24 | - If it does, return `false` as it violates the Sudoku rule.
|
23 | 25 | - If it does not, add the character to the respective sets.
|
24 | 26 |
|
25 |
| -**Final Check:** If we complete the iteration without finding any duplicates, we return `true`, indicating the board is |
| 27 | +**Final Check:** If we complete the iteration without finding any duplicates, we return `true`, indicating the board is |
26 | 28 | valid.
|
27 | 29 |
|
28 | 30 | ## Complexity
|
29 | 31 |
|
30 | 32 | - **Time Complexity: O(1)**
|
31 |
| - - We always iterate over a fixed 9x9 board, making the iteration part of the solution constant in terms of time |
32 |
| -complexity. Each cell is visited once. |
33 |
| - - The operations of checking and adding elements to the sets are average `O(1)` operations. |
| 33 | + - We always iterate over a fixed 9x9 board, making the iteration part of the solution constant in terms of time |
| 34 | + complexity. Each cell is visited once. |
| 35 | + - The operations of checking and adding elements to the sets are average `O(1)` operations. |
34 | 36 | - **Space Complexity: O(1)**
|
35 |
| - - Although we use additional data structures (maps and sets), their sizes are fixed and proportional to the size of |
36 |
| -the board, which is constant (81 cells). |
37 |
| - - Each map (rows, cols, boxes) holds at most 9 sets, and each set can hold up to 9 elements, making the extra space |
38 |
| -used constant. |
| 37 | + - Although we use additional data structures (maps and sets), their sizes are fixed and proportional to the size of |
| 38 | + the board, which is constant (81 cells). |
| 39 | + - Each map (rows, cols, boxes) holds at most 9 sets, and each set can hold up to 9 elements, making the extra space |
| 40 | + used constant. |
39 | 41 |
|
40 | 42 | ## Code
|
41 | 43 |
|
|
0 commit comments