-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathValidSudoku.java
More file actions
25 lines (22 loc) · 823 Bytes
/
ValidSudoku.java
File metadata and controls
25 lines (22 loc) · 823 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
class Solution {
// TC : O(1) => O(9^2) => O(81)
public boolean isValidSudoku(char[][] board) {
Set<String> seen = new HashSet();
for (int i=0; i<9; ++i) {
for (int j=0; j<9; ++j) {
char number = board[i][j];
if (number != '.')
if (seen.contains(number + "_R_" + i) ||
seen.contains(number + "_C_ " + j) ||
seen.contains(number + "_B_" + i/3 + "_" + j/3)) {
return false;
} else {
seen.add(number + "_R_" + i);
seen.add(number + "_C_ " + j);
seen.add(number + "_B_" + i/3 + "_" + j/3);
}
}
}
return true;
}
}