Skip to content

Commit

Permalink
Create: 0036-valid-sudoku.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
NikSWE committed Jan 4, 2023
1 parent 65cac1e commit ed984b5
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions dart/0036-valid-sudoku.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// As the board size is fixed
// it will result in the following
// Time Complexity: O(1)
// Space Complexity: O(1)

class Solution {
bool isValidSudoku(List<List<String>> board) {
var rows = List.filled(9, 0);
var cols = List.filled(9, 0);
var grids = List.filled(9, 0);

for (int r = 0; r < 9; r++) {
for (int c = 0; c < 9; c++) {
if (board[r][c] == ".") continue;

var idx = int.parse(board[r][c]) - 1;

if (rows[r] & 1 << idx != 0) return false;
rows[r] |= 1 << idx;

if (cols[c] & 1 << idx != 0) return false;
cols[c] |= 1 << idx;

if (grids[r ~/ 3 * 3 + c ~/ 3] & 1 << idx != 0) return false;
grids[r ~/ 3 * 3 + c ~/ 3] |= 1 << idx;
}
}

return true;
}
}

0 comments on commit ed984b5

Please sign in to comment.