|
| 1 | +''' |
| 2 | +Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: |
| 3 | +
|
| 4 | +Each row must contain the digits 1-9 without repetition. |
| 5 | +Each column must contain the digits 1-9 without repetition. |
| 6 | +Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition. |
| 7 | +
|
| 8 | +A partially filled sudoku which is valid. |
| 9 | +
|
| 10 | +The Sudoku board could be partially filled, where empty cells are filled with the character '.'. |
| 11 | +
|
| 12 | +Example 1: |
| 13 | +
|
| 14 | +Input: |
| 15 | +[ |
| 16 | + ["5","3",".",".","7",".",".",".","."], |
| 17 | + ["6",".",".","1","9","5",".",".","."], |
| 18 | + [".","9","8",".",".",".",".","6","."], |
| 19 | + ["8",".",".",".","6",".",".",".","3"], |
| 20 | + ["4",".",".","8",".","3",".",".","1"], |
| 21 | + ["7",".",".",".","2",".",".",".","6"], |
| 22 | + [".","6",".",".",".",".","2","8","."], |
| 23 | + [".",".",".","4","1","9",".",".","5"], |
| 24 | + [".",".",".",".","8",".",".","7","9"] |
| 25 | +] |
| 26 | +Output: true |
| 27 | +Example 2: |
| 28 | +
|
| 29 | +Input: |
| 30 | +[ |
| 31 | + ["8","3",".",".","7",".",".",".","."], |
| 32 | + ["6",".",".","1","9","5",".",".","."], |
| 33 | + [".","9","8",".",".",".",".","6","."], |
| 34 | + ["8",".",".",".","6",".",".",".","3"], |
| 35 | + ["4",".",".","8",".","3",".",".","1"], |
| 36 | + ["7",".",".",".","2",".",".",".","6"], |
| 37 | + [".","6",".",".",".",".","2","8","."], |
| 38 | + [".",".",".","4","1","9",".",".","5"], |
| 39 | + [".",".",".",".","8",".",".","7","9"] |
| 40 | +] |
| 41 | +Output: false |
| 42 | +Explanation: Same as Example 1, except with the 5 in the top left corner being |
| 43 | + modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid. |
| 44 | +Note: |
| 45 | +
|
| 46 | +A Sudoku board (partially filled) could be valid but is not necessarily solvable. |
| 47 | +Only the filled cells need to be validated according to the mentioned rules. |
| 48 | +The given board contain only digits 1-9 and the character '.'. |
| 49 | +The given board size is always 9x9. |
| 50 | +''' |
| 51 | + |
| 52 | +class Solution(object): |
| 53 | + def isValidSudoku(self, board): |
| 54 | + """ |
| 55 | + :type board: List[List[str]] |
| 56 | + :rtype: bool |
| 57 | + """ |
| 58 | + import collections |
| 59 | + dict_row, dict_col, dict_cell = collections.defaultdict(set), collections.defaultdict(set), collections.defaultdict(set) |
| 60 | + |
| 61 | + for row_index in range(1, 4): |
| 62 | + for col_index in range(1, 4): |
| 63 | + for row in range(3*(row_index-1), 3*row_index): |
| 64 | + for col in range(3*(col_index-1), 3*col_index): |
| 65 | + cell_data = board[row][col] |
| 66 | + if cell_data == '.': |
| 67 | + continue |
| 68 | + if cell_data in dict_row[row] or cell_data in dict_col[col] or cell_data in dict_cell[(row_index, col_index)]: |
| 69 | + return False |
| 70 | + |
| 71 | + dict_row[row].add(cell_data) |
| 72 | + dict_col[col].add(cell_data) |
| 73 | + dict_cell[(row_index, col_index)].add(cell_data) |
| 74 | + |
| 75 | + return True |
0 commit comments