valid-sudoku py3
This commit is contained in:
parent
80245886a1
commit
fb6e386479
49
0036_valid-sudoku/README.md
Normal file
49
0036_valid-sudoku/README.md
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
Determine if a `9 x 9` Sudoku board is valid. Only the filled cells need to be validated **according to the following rules**:
|
||||||
|
|
||||||
|
1. Each row must contain the digits `1-9` without repetition.
|
||||||
|
2. Each column must contain the digits `1-9` without repetition.
|
||||||
|
3. Each of the nine `3 x 3` sub-boxes of the grid must contain the digits `1-9` without repetition.
|
||||||
|
|
||||||
|
**Note:**
|
||||||
|
|
||||||
|
* A Sudoku board (partially filled) could be valid but is not necessarily solvable.
|
||||||
|
* Only the filled cells need to be validated according to the mentioned rules.
|
||||||
|
|
||||||
|
**Example 1:**
|
||||||
|
|
||||||
|
![](https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png)
|
||||||
|
|
||||||
|
Input: board =
|
||||||
|
[["5","3",".",".","7",".",".",".","."]
|
||||||
|
,["6",".",".","1","9","5",".",".","."]
|
||||||
|
,[".","9","8",".",".",".",".","6","."]
|
||||||
|
,["8",".",".",".","6",".",".",".","3"]
|
||||||
|
,["4",".",".","8",".","3",".",".","1"]
|
||||||
|
,["7",".",".",".","2",".",".",".","6"]
|
||||||
|
,[".","6",".",".",".",".","2","8","."]
|
||||||
|
,[".",".",".","4","1","9",".",".","5"]
|
||||||
|
,[".",".",".",".","8",".",".","7","9"]]
|
||||||
|
Output: true
|
||||||
|
|
||||||
|
|
||||||
|
**Example 2:**
|
||||||
|
|
||||||
|
Input: board =
|
||||||
|
[["8","3",".",".","7",".",".",".","."]
|
||||||
|
,["6",".",".","1","9","5",".",".","."]
|
||||||
|
,[".","9","8",".",".",".",".","6","."]
|
||||||
|
,["8",".",".",".","6",".",".",".","3"]
|
||||||
|
,["4",".",".","8",".","3",".",".","1"]
|
||||||
|
,["7",".",".",".","2",".",".",".","6"]
|
||||||
|
,[".","6",".",".",".",".","2","8","."]
|
||||||
|
,[".",".",".","4","1","9",".",".","5"]
|
||||||
|
,[".",".",".",".","8",".",".","7","9"]]
|
||||||
|
Output: false
|
||||||
|
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.
|
||||||
|
|
||||||
|
|
||||||
|
**Constraints:**
|
||||||
|
|
||||||
|
* `board.length == 9`
|
||||||
|
* `board[i].length == 9`
|
||||||
|
* `board[i][j]` is a digit `1-9` or `'.'`.
|
54
0036_valid-sudoku/python3/solution.py
Normal file
54
0036_valid-sudoku/python3/solution.py
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
class Solution:
|
||||||
|
def isValidSudoku(self, board: List[List[str]]) -> bool:
|
||||||
|
#
|
||||||
|
# NOTE: **READ THE PROBLEM DESCRIPTION ALWAYS!!**
|
||||||
|
#
|
||||||
|
# Even though it mentions Sudoku, it doesn't mean the
|
||||||
|
# rules are the same as the real world. Which is the case
|
||||||
|
# here — we need to only look at the filled cell and the rules
|
||||||
|
# provided.
|
||||||
|
|
||||||
|
size = len(board)
|
||||||
|
|
||||||
|
# 1. Check if rows and cols are valid
|
||||||
|
#
|
||||||
|
# We can iterate both rows and cols at the same time since
|
||||||
|
# the sudoku board has 9 rows and 9 cols
|
||||||
|
for i in range(size):
|
||||||
|
row = set()
|
||||||
|
col = set()
|
||||||
|
|
||||||
|
for j in range(size):
|
||||||
|
# ith row check
|
||||||
|
if board[i][j] != '.':
|
||||||
|
if board[i][j] in row:
|
||||||
|
return False
|
||||||
|
|
||||||
|
row.add(board[i][j])
|
||||||
|
|
||||||
|
# ith col check
|
||||||
|
if board[j][i] != '.':
|
||||||
|
if board[j][i] in col:
|
||||||
|
return False
|
||||||
|
|
||||||
|
col.add(board[j][i])
|
||||||
|
|
||||||
|
# 2. Check if squares are valid
|
||||||
|
#
|
||||||
|
# We can do normal row * col iteration and use `// 3` to figure
|
||||||
|
# out which square a cell belongs to
|
||||||
|
|
||||||
|
squares = [[set(), set(), set()] for _ in range(3)]
|
||||||
|
for i in range(size):
|
||||||
|
for j in range(size):
|
||||||
|
cell = board[i][j]
|
||||||
|
if cell == '.':
|
||||||
|
continue
|
||||||
|
|
||||||
|
square = squares[i // 3][j // 3]
|
||||||
|
if cell in square:
|
||||||
|
return False
|
||||||
|
|
||||||
|
square.add(cell)
|
||||||
|
|
||||||
|
return True
|
Loading…
Reference in New Issue
Block a user