-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSudokuValidator.java
More file actions
73 lines (73 loc) · 1.6 KB
/
SudokuValidator.java
File metadata and controls
73 lines (73 loc) · 1.6 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* create an algorithm that checks any solved sudoku's validity and show where the repetition is
happening.
input:
3 5 2 9 1 8 6 7 4
8 9 7 2 4 6 5 1 3
6 4 1 7 5 3 2 8 9
7 8 3 5 6 9 4 2 1
9 2 6 1 3 4 7 5 8
4 1 5 8 2 7 9 3 6
8 6 4 3 7 5 8 9 2
2 7 8 4 9 1 3 6 5
5 3 9 6 8 2 1 4 7
output:
[7,7] = 8 is repeated
[7,1] = 8 is repeated
false
input:
3 5 2 9 1 8 6 7 4
8 9 7 2 4 6 5 1 3
6 4 1 7 5 3 2 8 9
7 8 3 5 6 9 4 2 1
9 2 6 1 3 4 7 5 8
4 1 5 8 2 7 9 3 6
1 6 4 3 7 5 8 9 2
2 7 8 4 9 1 3 6 5
5 3 9 6 8 2 1 4 7
output:
true
*/
import java.util.*;
public class SudokuValidator{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int sudoku[][] = new int[9][9];
for (int i=0;i<9 ;i++ ) {
for (int j=0;j<9 ;j++ ) {
sudoku[i][j] = in.nextInt();
}
}
boolean check = true;
/*Column wise checking*/
String res = "";
for (int i=0;i<9 ;i++ ) {
for (int j=0;j<9 ;j++ ) {
if(res.contains(Integer.toString(sudoku[i][j]))){
System.out.println("["+(i+1)+","+(j+1)+"] = "+sudoku[i][j]+" is repeated");
check = false;
}else{
res+=Integer.toString(sudoku[i][j]);
}
}
res = "";
}
res = "";
/*Row wise checking*/
for (int i=0;i<9 ;i++ ) {
for (int j=0;j<9 ;j++ ) {
if(res.contains(Integer.toString(sudoku[j][i]))){
System.out.println("["+(j+1)+","+(i+1)+"] = "+sudoku[j][i]+" is repeated");
check = false;
}else{
res+=Integer.toString(sudoku[j][i]);
}
}
res = "";
}
if (check) {
System.out.println(true);
}else{
System.out.println(false);
}
}
}