-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathValidateSudoku.java
41 lines (40 loc) · 1.42 KB
/
ValidateSudoku.java
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
40
41
import java.util.*;
public class Solution {
public boolean isValidSudoku(char[][] board) {
int SIZE = board.length;
// Start typing your Java solution below
// DO NOT write main() function
HashSet<Character> validator = new HashSet<Character>();
// Row
for (int i = 0; i < SIZE; i++){
validator.clear();
for (int j = 0; j < SIZE; j++){
if (board[i][j] == '.') continue;
if(validator.contains(board[i][j])) return false;
validator.add(board[i][j]);
}
}
// Column
for (int j = 0; j < SIZE; j++){
validator.clear();
for (int i = 0; i < SIZE; i++){
if (board[i][j] == '.') continue;
if(validator.contains(board[i][j])) return false;
validator.add(board[i][j]);
}
}
for (int i = 0; i < SIZE; i += 3){
for (int j = 0; j < SIZE; j += 3){
validator.clear();
for (int m = 0; m < 3; m ++){
for (int n = 0; n < 3; n ++){
if (board[i + m][j + n] == '.') continue;
if(validator.contains(board[i + m][j + n])) return false;
validator.add(board[i + m][j + n]);
}
}
}
}
return true;
}
}