We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 65cac1e commit ed984b5Copy full SHA for ed984b5
dart/0036-valid-sudoku.dart
@@ -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