Skip to content

Commit aabb414

Browse files
committed
Update valid sedoku after solving it again
1 parent 31d92af commit aabb414

File tree

2 files changed

+39
-30
lines changed

2 files changed

+39
-30
lines changed

.pylintrc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ init-hook="import sys; sys.path.append('./')"
33
ignore=tests
44

55
[MESSAGES CONTROL]
6-
disable=C0111, # missing-docstring
6+
disable=C0103, # invalid-name
7+
C0111, # missing-docstring
8+
C0200, # consider-using-enumerate
79
C0301, # line-too-long
810
R0903, # too-few-public-methods
911

src/leetcode/medium/valid-sedoku/valid_sedoku.py

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
Only the filled cells need to be validated according to the mentioned rules.
1111
1212
Example 1:
13-
Input: board =
13+
Input: board =
1414
[["5","3",".",".","7",".",".",".","."]
1515
,["6",".",".","1","9","5",".",".","."]
1616
,[".","9","8",".",".",".",".","6","."]
@@ -23,7 +23,7 @@
2323
Output: true
2424
2525
Example 2:
26-
Input: board =
26+
Input: board =
2727
[["8","3",".",".","7",".",".",".","."]
2828
,["6",".",".","1","9","5",".",".","."]
2929
,[".","9","8",".",".",".",".","6","."]
@@ -44,34 +44,41 @@
4444

4545
from typing import List
4646

47+
4748
def isValidSudoku(board: List[List[str]]) -> bool:
48-
prevSeen = {
49-
"rows": { num: {} for num in range(len(board)) },
50-
"columns": { num: {} for num in range(len(board[0])) }
51-
}
52-
buckets = [[{} for _ in range(3)] for _ in range(3)]
53-
54-
for y in range(len(board)):
55-
for x in range(len(board[y])):
56-
currentNum = board[y][x]
57-
58-
if (currentNum == "."):
49+
rowToNums = {}
50+
columnToNums = {}
51+
gridToNums = {}
52+
53+
for y, _ in enumerate(board):
54+
for x, column in enumerate(board[y]):
55+
# Skip "." values which aren't necessary to check
56+
if column == ".":
5957
continue
60-
61-
if currentNum in prevSeen["rows"][y]:
62-
return False
63-
if currentNum in prevSeen["columns"][x]:
58+
59+
# Set initial tracker keys if not present
60+
if not y in rowToNums:
61+
rowToNums[y] = {}
62+
if not x in columnToNums:
63+
columnToNums[x] = {}
64+
65+
yGridBox = y // 3
66+
xGridBox = x // 3
67+
gridKey = f"{yGridBox}{xGridBox}"
68+
if gridKey not in gridToNums:
69+
gridToNums[gridKey] = {}
70+
71+
# Perform check for num presence in sub-box grid
72+
if column in gridToNums[gridKey]:
6473
return False
65-
66-
prevSeen["rows"][y][currentNum] = True
67-
prevSeen["columns"][x][currentNum] = True
68-
69-
bucketY = y // 3
70-
bucketX = x // 3
71-
72-
if currentNum in buckets[bucketY][bucketX]:
74+
75+
# Next, perform checks for row/column values
76+
if column in rowToNums[y] or column in columnToNums[x]:
7377
return False
74-
75-
buckets[bucketY][bucketX][currentNum] = True
76-
77-
return True
78+
79+
# Set values in trackers for later comparisons
80+
rowToNums[y][column] = True
81+
columnToNums[x][column] = True
82+
gridToNums[gridKey][column] = True
83+
84+
return True

0 commit comments

Comments
 (0)