Skip to content

Commit ed984b5

Browse files
committed
Create: 0036-valid-sudoku.dart
1 parent 65cac1e commit ed984b5

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

dart/0036-valid-sudoku.dart

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// As the board size is fixed
2+
// it will result in the following
3+
// Time Complexity: O(1)
4+
// Space Complexity: O(1)
5+
6+
class Solution {
7+
bool isValidSudoku(List<List<String>> board) {
8+
var rows = List.filled(9, 0);
9+
var cols = List.filled(9, 0);
10+
var grids = List.filled(9, 0);
11+
12+
for (int r = 0; r < 9; r++) {
13+
for (int c = 0; c < 9; c++) {
14+
if (board[r][c] == ".") continue;
15+
16+
var idx = int.parse(board[r][c]) - 1;
17+
18+
if (rows[r] & 1 << idx != 0) return false;
19+
rows[r] |= 1 << idx;
20+
21+
if (cols[c] & 1 << idx != 0) return false;
22+
cols[c] |= 1 << idx;
23+
24+
if (grids[r ~/ 3 * 3 + c ~/ 3] & 1 << idx != 0) return false;
25+
grids[r ~/ 3 * 3 + c ~/ 3] |= 1 << idx;
26+
}
27+
}
28+
29+
return true;
30+
}
31+
}

0 commit comments

Comments
 (0)