|
| 1 | +### [36\. Valid SudokuCopy for MarkdownCopy for MarkdownCopy for Markdown](https://leetcode.com/problems/valid-sudoku/) |
| 2 | + |
| 3 | +Difficulty: **Medium** |
| 4 | + |
| 5 | + |
| 6 | +Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated **according to the following rules**: |
| 7 | + |
| 8 | +1. Each row must contain the digits `1-9` without repetition. |
| 9 | +2. Each column must contain the digits `1-9` without repetition. |
| 10 | +3. Each of the 9 `3x3` sub-boxes of the grid must contain the digits `1-9` without repetition. |
| 11 | + |
| 12 | + |
| 13 | +<small style="display: inline;">A partially filled sudoku which is valid.</small> |
| 14 | + |
| 15 | +The Sudoku board could be partially filled, where empty cells are filled with the character `'.'`. |
| 16 | + |
| 17 | +**Example 1:** |
| 18 | + |
| 19 | +``` |
| 20 | +Input: |
| 21 | +[ |
| 22 | + ["5","3",".",".","7",".",".",".","."], |
| 23 | + ["6",".",".","1","9","5",".",".","."], |
| 24 | + [".","9","8",".",".",".",".","6","."], |
| 25 | + ["8",".",".",".","6",".",".",".","3"], |
| 26 | + ["4",".",".","8",".","3",".",".","1"], |
| 27 | + ["7",".",".",".","2",".",".",".","6"], |
| 28 | + [".","6",".",".",".",".","2","8","."], |
| 29 | + [".",".",".","4","1","9",".",".","5"], |
| 30 | + [".",".",".",".","8",".",".","7","9"] |
| 31 | +] |
| 32 | +Output: true |
| 33 | +``` |
| 34 | + |
| 35 | +**Example 2:** |
| 36 | + |
| 37 | +``` |
| 38 | +Input: |
| 39 | +[ |
| 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 | +Explanation: Same as Example 1, except with the 5 in the top left corner being |
| 52 | + modified to 8\. Since there are two 8's in the top left 3x3 sub-box, it is invalid. |
| 53 | +``` |
| 54 | + |
| 55 | +**Note:** |
| 56 | + |
| 57 | +* A Sudoku board (partially filled) could be valid but is not necessarily solvable. |
| 58 | +* Only the filled cells need to be validated according to the mentioned rules. |
| 59 | +* The given board contain only digits `1-9` and the character `'.'`. |
| 60 | +* The given board size is always `9x9`. |
| 61 | + |
| 62 | + |
| 63 | +#### Solution |
| 64 | + |
| 65 | +Language: **Java** |
| 66 | + |
| 67 | +```java |
| 68 | +class Solution { |
| 69 | + public boolean isValidSudoku(char[][] board) { |
| 70 | + Set<String> seen = new HashSet<>(); |
| 71 | + for (int i = 0; i < 9; i++) { |
| 72 | + for (int j = 0; j < 9; j++) { |
| 73 | + char num = board[i][j]; |
| 74 | + if (num != '.') { |
| 75 | + if (!seen.add(num + " in row " + i) |
| 76 | + || !seen.add(num + " in column " + j) |
| 77 | + || !seen.add(num + " in block " + i / 3 + "," + j / 3)) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + return true; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +``` |
| 88 | + |
0 commit comments