-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvalid_sudoku.dart
157 lines (131 loc) · 4.4 KB
/
valid_sudoku.dart
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
/*
-* 36. Valid Sudoku *-
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 'dart:collection';
class A {
bool isValidSudoku(List<List<String>> board) {
HashSet<String> hashSet = HashSet();
for (int row = 0; row < 9; row++) {
for (int column = 0; column < 9; column++) {
String number = board[row][column];
if (number != '.') {
if (!hashSet.add(number + " in row " + String.fromCharCode(row)) ||
!hashSet
.add(number + " in column " + String.fromCharCode(column)) ||
!hashSet.add(
number +
" in block " +
String.fromCharCode(row ~/ 3) +
"," +
String.fromCharCode(column ~/ 3),
)) return false;
}
}
}
return true;
}
}
class B {
bool isValidSudoku(List<List<String>> board) {
for (int row = 0; row < 9; row++) {
for (int column = 0; column < 9; column++) {
if (board[row][column] != '.' &&
!isValidPlacement(board, board[row][column], row, column))
return false;
}
}
return true;
}
bool isNumberInRow(
List<List<String>> board, String number, int row, int column) {
for (int i = 0; i < 9; i++) {
if (column != i && board[row][i] == number) return true;
}
return false;
}
bool isNumberInColumn(
List<List<String>> board, String number, int row, int column) {
for (int i = 0; i < 9; i++) {
if (row != i && board[i][column] == number) return true;
}
return false;
}
bool isNumberInBox(
List<List<String>> board, String number, int row, int column) {
int boxRow = row - row % 3;
int boxColumn = column - column % 3;
for (int i = boxRow; i < boxRow + 3; i++) {
for (int j = boxColumn; j < boxColumn + 3; j++) {
if (row != i && column != j && board[i][j] == number) return true;
}
}
return false;
}
bool isValidPlacement(
List<List<String>> board, String number, int row, int column) {
return !isNumberInRow(board, number, row, column) &&
!isNumberInColumn(board, number, row, column) &&
!isNumberInBox(board, number, row, column);
}
}
class C {
int getBit(int x, int k) {
return (x >> k) & 1;
}
bool isValidSudoku(List<List<String>> board) {
int n = 9;
List<int> rows = List.filled(n, 0);
List<int> cols = List.filled(n, 0);
List<int> squares = List.filled(n, 0);
for (int r = 0; r < n; r++) {
for (int c = 0; c < n; c++) {
if (board[r][c] == '.') continue;
int val = board[r][c].codeUnitAt(0) - '0'.codeUnitAt(0);
int sPos = (r ~/ 3) * 3 + (c ~/ 3);
if (getBit(rows[r], val) == 1 ||
getBit(cols[c], val) == 1 ||
getBit(squares[sPos], val) == 1) return false;
rows[r] |= 1 << val;
cols[c] |= 1 << val;
squares[sPos] |= 1 << val;
}
}
return true;
}
}