-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidSuduko.java
More file actions
26 lines (22 loc) · 928 Bytes
/
Copy pathValidSuduko.java
File metadata and controls
26 lines (22 loc) · 928 Bytes
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
//Time Complexity: O(1) - The board size is fixed (9x9), so the time complexity is constant.
//Space Complexity: O(1) - The additional space used is also constant due to the fixed size of the board.
class Solution {
public boolean isValidSudoku(char[][] board) {
boolean[][] rows = new boolean[9][9];
boolean[][] cols = new boolean[9][9];
boolean[][] boxes = new boolean[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] != '.') {
int num = board[i][j] - '1';
int boxIndex = (i / 3) * 3 + (j / 3);
if (rows[i][num] || cols[j][num] || boxes[boxIndex][num]) {
return false;
}
rows[i][num] = cols[j][num] = boxes[boxIndex][num] = true;
}
}
}
return true;
}
}