File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments