Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7afcdb1

Browse files
36. Valid Sudoku
1 parent 428fb15 commit 7afcdb1

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
'''
2+
36. Valid Sudoku
3+
4+
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
5+
1. Each row must contain the digits 1-9 without repetition.
6+
2. Each column must contain the digits 1-9 without repetition.
7+
3. Each of the nine 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.
8+
9+
Note:
10+
A Sudoku board (partially filled) could be valid but is not necessarily solvable.
11+
Only the filled cells need to be validated according to the mentioned rules.
12+
13+
Example 1:
14+
Input: board =
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+
28+
Example 2:
29+
Input: board =
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+
'''
43+
44+
# Approach:
45+
# Time Complexity: O(N^2) where N is the number of cells in the board (81 cells).
46+
# Space Complexity: O(N^2) for the sets used to track seen numbers in rows, columns, and boxes.
47+
'''
48+
We can use a set to keep track of the numbers we have seen in each row, column, and 3x3 sub-box.
49+
We will iterate through each cell in the board and check if the number is already in the corresponding row, column, or sub-box set.
50+
If it is, we return false. If not, we add the number to the respective sets.
51+
'''
52+
53+
class Solution:
54+
def isValidSudoku(self, board):
55+
row_count={}
56+
col_count={}
57+
box_count={}
58+
59+
for r in range(9):
60+
for c in range(9):
61+
num = board[r][c]
62+
63+
if num ==".":
64+
continue
65+
bx_in= (r//3)*3+(c//3)
66+
if r not in row_count:
67+
row_count[r]=set()
68+
if c not in col_count:
69+
col_count[c]=set()
70+
if bx_in not in box_count:
71+
box_count[bx_in]=set()
72+
73+
if num in row_count[r] or num in col_count[c] or num in box_count[bx_in]:
74+
return False
75+
76+
row_count[r].add(num)
77+
col_count[c].add(num)
78+
box_count[bx_in].add(num)
79+
80+
return True
81+
82+
83+
84+
obj = Solution()
85+
board =[["1","2",".",".","3",".",".",".","."],
86+
["4",".",".","5",".",".",".",".","."],
87+
[".","9","8",".",".",".",".",".","3"],
88+
["5",".",".",".","6",".",".",".","4"],
89+
[".",".",".","8",".","3",".",".","5"],
90+
["7",".",".",".","2",".",".",".","6"],
91+
[".",".",".",".",".",".","2",".","."],
92+
[".",".",".","4","1","9",".",".","8"],
93+
[".",".",".",".","8",".",".","7","9"]]
94+
95+
print(obj.isValidSudoku(board))
96+
97+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /