|
| 1 | +36\. Valid Sudoku |
| 2 | + |
| 3 | +Medium |
| 4 | + |
| 5 | +Determine if a `9 x 9` Sudoku board is valid. Only the filled cells need to be validated **according to the following rules**: |
| 6 | + |
| 7 | +1. Each row must contain the digits `1-9` without repetition. |
| 8 | +2. Each column must contain the digits `1-9` without repetition. |
| 9 | +3. Each of the nine `3 x 3` sub-boxes of the grid must contain the digits `1-9` without repetition. |
| 10 | + |
| 11 | +**Note:** |
| 12 | + |
| 13 | +* A Sudoku board (partially filled) could be valid but is not necessarily solvable. |
| 14 | +* Only the filled cells need to be validated according to the mentioned rules. |
| 15 | + |
| 16 | +**Example 1:** |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +**Input:** |
| 21 | + |
| 22 | + board = |
| 23 | + [["5","3",".",".","7",".",".",".","."] |
| 24 | + ,["6",".",".","1","9","5",".",".","."] |
| 25 | + ,[".","9","8",".",".",".",".","6","."] |
| 26 | + ,["8",".",".",".","6",".",".",".","3"] |
| 27 | + ,["4",".",".","8",".","3",".",".","1"] |
| 28 | + ,["7",".",".",".","2",".",".",".","6"] |
| 29 | + ,[".","6",".",".",".",".","2","8","."] |
| 30 | + ,[".",".",".","4","1","9",".",".","5"] |
| 31 | + ,[".",".",".",".","8",".",".","7","9"]] |
| 32 | + |
| 33 | +**Output:** true |
| 34 | + |
| 35 | +**Example 2:** |
| 36 | + |
| 37 | +**Input:** |
| 38 | + |
| 39 | + board = |
| 40 | + [["8","3",".",".","7",".",".",".","."] |
| 41 | + ,["6",".",".","1","9","5",".",".","."] |
| 42 | + ,[".","9","8",".",".",".",".","6","."] |
| 43 | + ,["8",".",".",".","6",".",".",".","3"] |
| 44 | + ,["4",".",".","8",".","3",".",".","1"] |
| 45 | + ,["7",".",".",".","2",".",".",".","6"] |
| 46 | + ,[".","6",".",".",".",".","2","8","."] |
| 47 | + ,[".",".",".","4","1","9",".",".","5"] |
| 48 | + ,[".",".",".",".","8",".",".","7","9"]] |
| 49 | + |
| 50 | +**Output:** false |
| 51 | + |
| 52 | +**Explanation:** Same as Example 1, except with the **5** in the top left corner being modified to **8**. Since there are two 8's in the top left 3x3 sub-box, it is invalid. |
| 53 | + |
| 54 | +**Constraints:** |
| 55 | + |
| 56 | +* `board.length == 9` |
| 57 | +* `board[i].length == 9` |
| 58 | +* `board[i][j]` is a digit `1-9` or `'.'`. |
0 commit comments