Skip to content

Commit 4e179ac

Browse files
authored
Create Valid Sudoku.java
1 parent 98f0f85 commit 4e179ac

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Medium/Valid Sudoku.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public boolean isValidSudoku(char[][] board) {
3+
HashSet<Character>[] rowSet = new HashSet[9];
4+
HashSet<Character>[] colSet = new HashSet[9];
5+
HashSet<Character>[] boxSet = new HashSet[9];
6+
for (int i = 0; i < 9; i++) {
7+
rowSet[i] = new HashSet<>();
8+
colSet[i] = new HashSet<>();
9+
boxSet[i] = new HashSet<>();
10+
}
11+
for (int i = 0; i < 9; i++) {
12+
for (int j = 0; j < 9; j++) {
13+
if (board[i][j] != '.') {
14+
char val = board[i][j];
15+
int boxIdx = (i / 3) * 3 + j / 3;
16+
if (rowSet[i].contains(val) || colSet[j].contains(val) || boxSet[boxIdx].contains(val)) {
17+
return false;
18+
}
19+
rowSet[i].add(val);
20+
colSet[j].add(val);
21+
boxSet[boxIdx].add(val);
22+
}
23+
}
24+
}
25+
return true;
26+
}
27+
}

0 commit comments

Comments
 (0)