-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidSudoko.java
50 lines (42 loc) · 1.64 KB
/
ValidSudoko.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
42
43
44
45
46
47
48
49
50
import java.util.HashSet;
import java.util.Set;
public class ValidSudoko {
public boolean isValidSudoku(char[][] board) {
Set<String> set = new HashSet<>();
String row = "row";
String col = "col";
String square = "square";
String valueSeparator = "|";
for(int i=0; i<board.length; i++){
for(int j=0; j<board.length; j++){
if(board[i][j] == '.')
continue;
String rowVal = row + i + valueSeparator + board[i][j];
if(!set.add(rowVal))
return false;
String colVal = col + j + valueSeparator + board[i][j];
if(!set.add(colVal))
return false;
String squareVal = square + i/3 + "" +j/3 + valueSeparator + board[i][j];
if(!set.add(squareVal))
return false;
}
}
return true;
}
public static void main(String[] args) {
char [][]board = new char[][] {
{'5','3','.','.','7','.','.','.','.'}
,{'6','.','.','1','9','5','.','.','.'}
,{'.','9','8','.','.','.','.','6','.'}
,{'8','.','.','.','6','.','.','.','3'}
,{'4','.','.','8','.','3','.','.','1'}
,{'7','.','.','.','2','.','.','.','6'}
,{'.','6','.','.','.','.','2','8','.'}
,{'.','.','.','4','1','9','.','.','5'}
,{'.','.','.','.','8','.','.','7','9'}};
ValidSudoko sudokoSolver = new ValidSudoko();
boolean isValid = sudokoSolver.isValidSudoku(board);
System.out.println(isValid);
}
}