-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0036.valid-sudoku.py
258 lines (235 loc) · 9.42 KB
/
0036.valid-sudoku.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# Category: algorithms
# Level: Medium
# Percent: 59.169662%
# Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
#
#
# Each row must contain the digits 1-9 without repetition.
# Each column must contain the digits 1-9 without repetition.
# 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:
#
# 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 '.'.
#
import unittest
from typing import List
# start_marker
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
def cell_to_box(i, j):
return (i // 3) * 3 + j // 3
rows = [set() for _ in range(9)]
columns = [set() for _ in range(9)]
boxes = [set() for _ in range(9)]
for i in range(9):
for j in range(9):
element = board[i][j]
if element == ".":
continue
if (
element in rows[i]
or element in columns[j]
or element in boxes[cell_to_box(i, j)]
):
return False
rows[i].add(element)
columns[j].add(element)
boxes[cell_to_box(i, j)].add(element)
return True
def isValidSudoku_old(self, board: List[List[str]]) -> bool:
def get_three_by_three(board, i):
start_x = i // 3 * 3
start_y = i % 3 * 3
three_by_three = []
for x in range(0, 3):
for y in range(0, 3):
element = board[start_x + x][start_y + y]
if element != ".":
three_by_three.append(element)
return three_by_three
def validate(box):
if not box:
return True
return len(box) == len(set(box))
for i in range(len(board)):
row = [x for x in board[i] if x != "."]
if not validate(row):
return False
column = [x[i] for x in board if x[i] != "."]
if not validate(column):
return False
three_by_three = get_three_by_three(board, i)
if not validate(three_by_three):
return False
return True
# end_marker
class TestSolution(unittest.TestCase):
def test_case_1(self):
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"],
]
result = Solution().isValidSudoku(board)
self.assertEqual(result, True)
def test_case_2(self):
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"],
]
result = Solution().isValidSudoku(board)
self.assertEqual(result, False)
def test_case_3(self):
"""completely empty board"""
board = [["." for _ in range(9)] for _ in range(9)]
result = Solution().isValidSudoku(board)
self.assertEqual(result, True)
def test_case_4(self):
"""completely full board"""
board = [
["1", "2", "3", "4", "5", "6", "7", "8", "9"],
["4", "5", "6", "7", "8", "9", "1", "2", "3"],
["7", "8", "9", "1", "2", "3", "4", "5", "6"],
["2", "3", "4", "5", "6", "7", "8", "9", "1"],
["5", "6", "7", "8", "9", "1", "2", "3", "4"],
["8", "9", "1", "2", "3", "4", "5", "6", "7"],
["3", "4", "5", "6", "7", "8", "9", "1", "2"],
["6", "7", "8", "9", "1", "2", "3", "4", "5"],
["9", "1", "2", "3", "4", "5", "6", "7", "8"],
]
result = Solution().isValidSudoku(board)
self.assertEqual(result, True)
def test_case_5(self):
"""completely full board with one invalid row"""
board = [
["1", "2", "3", "4", "5", "6", "7", "8", "9"],
["4", "5", "6", "7", "8", "9", "1", "2", "3"],
["7", "8", "9", "1", "2", "3", "4", "5", "6"],
["2", "3", "4", "5", "6", "7", "8", "9", "1"],
["5", "6", "7", "8", "9", "1", "2", "3", "4"],
["8", "9", "1", "2", "3", "4", "5", "6", "7"],
["3", "4", "5", "6", "7", "8", "9", "1", "2"],
["6", "7", "8", "9", "1", "2", "3", "4", "5"],
["9", "1", "2", "3", "4", "5", "6", "7", "1"],
]
result = Solution().isValidSudoku(board)
self.assertEqual(result, False)
def test_case_6(self):
"""completely full board with one invalid column"""
board = [
["1", "2", "3", "4", "5", "6", "7", "8", "9"],
["4", "5", "6", "7", "8", "9", "1", "2", "3"],
["7", "8", "9", "1", "2", "3", "4", "5", "6"],
["2", "3", "4", "5", "6", "7", "8", "9", "1"],
["5", "6", "7", "8", "9", "1", "2", "3", "4"],
["8", "9", "1", "2", "3", "4", "5", "6", "7"],
["3", "4", "5", "6", "7", "8", "9", "1", "2"],
["6", "7", "8", "9", "1", "2", "3", "4", "5"],
["9", "1", "2", "3", "4", "5", "6", "7", "1"],
]
result = Solution().isValidSudoku(board)
self.assertEqual(result, False)
def test_case_7(self):
"""completely full board with one invalid 3x3 box"""
board = [
["1", "2", "3", "4", "5", "6", "7", "8", "9"],
["4", "5", "6", "7", "8", "9", "1", "2", "3"],
["7", "8", "9", "1", "2", "3", "4", "5", "6"],
["2", "3", "4", "5", "6", "7", "8", "9", "1"],
["5", "6", "7", "8", "9", "1", "2", "3", "4"],
["8", "9", "1", "2", "3", "4", "5", "6", "7"],
["3", "4", "5", "6", "7", "8", "9", "1", "2"],
["6", "7", "8", "9", "1", "2", "3", "4", "5"],
["9", "1", "2", "3", "4", "5", "6", "7", "1"],
]
result = Solution().isValidSudoku(board)
self.assertEqual(result, False)
def test_case_8(self):
"""board with one element in each row, column, and 3x3 box"""
board = [
["1", ".", ".", ".", ".", ".", ".", ".", "."],
[".", "2", ".", ".", ".", ".", ".", ".", "."],
[".", ".", "3", ".", ".", ".", ".", ".", "."],
[".", ".", ".", "4", ".", ".", ".", ".", "."],
[".", ".", ".", ".", "5", ".", ".", ".", "."],
[".", ".", ".", ".", ".", "6", ".", ".", "."],
[".", ".", ".", ".", ".", ".", "7", ".", "."],
[".", ".", ".", ".", ".", ".", ".", "8", "."],
[".", ".", ".", ".", ".", ".", ".", ".", "9"],
]
result = Solution().isValidSudoku(board)
self.assertEqual(result, True)
def test_case_9(self):
board = [
[".", ".", ".", ".", "5", ".", ".", "1", "."],
[".", "4", ".", "3", ".", ".", ".", ".", "."],
[".", ".", ".", ".", ".", "3", ".", ".", "1"],
["8", ".", ".", ".", ".", ".", ".", "2", "."],
[".", ".", "2", ".", "7", ".", ".", ".", "."],
[".", "1", "5", ".", ".", ".", ".", ".", "."],
[".", ".", ".", ".", ".", "2", ".", ".", "."],
[".", "2", ".", "9", ".", ".", ".", ".", "."],
[".", ".", "4", ".", ".", ".", ".", ".", "."],
]
result = Solution().isValidSudoku(board)
self.assertEqual(result, False)
if __name__ == "__main__":
unittest.main()