-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathValidSydoku.java
More file actions
39 lines (35 loc) · 1.27 KB
/
ValidSydoku.java
File metadata and controls
39 lines (35 loc) · 1.27 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.HashSet;
import java.util.Set;
class Solution {
public boolean isValidSudoku(char[][] board) {
for (int row = 0; row < 9; row++) {
Set<Character> seen = new HashSet<>();
for (int i = 0; i < 9; i++) {
if (board[row][i] == '.') continue;
if (seen.contains(board[row][i])) return false;
seen.add(board[row][i]);
}
}
for (int col = 0; col < 9; col++) {
Set<Character> seen = new HashSet<>();
for (int i = 0; i < 9; i++) {
if (board[i][col] == '.') continue;
if (seen.contains(board[i][col])) return false;
seen.add(board[i][col]);
}
}
for (int square = 0; square < 9; square++) {
Set<Character> seen = new HashSet<>();
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
int row = (square / 3) * 3 + i;
int col = (square % 3) * 3 + j;
if (board[row][col] == '.') continue;
if (seen.contains(board[row][col])) return false;
seen.add(board[row][col]);
}
}
}
return true;
}
}