-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP5_17.java
More file actions
62 lines (53 loc) · 1.03 KB
/
P5_17.java
File metadata and controls
62 lines (53 loc) · 1.03 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
import java.util.*;
import java.util.List;
public class P5_17 {
public static void main(String[] args) {
}
public boolean isValidSudoku(char[][] board)
{
//Check rows
for(char[] bd : board)
{
if(!checkDuplicates(bd)) return false;
}
//Check Columns
for(int i = 0; i < 9; i++)
{
char[] col = new char[9];
int idx = 0;
for(char[] bd: board)
{
col[idx++] = bd[i];
}
if(!checkDuplicates(col)) return false;
}
//Check all
for(int k = 0; k < 3; k ++)
{
for(int j = 0; j < 3; j++)
{
char[] col = new char[9];
int idx = 0;
for(int i = (k * 3); i < ((k + 1) * 3); i++)
{
for(int q = (j * 3); q < ((j + 1) * 3); q++)
{
col[idx++] = board[i][q];
}
}
if(!checkDuplicates(col)) return false;
}
}
return true;
}
public static boolean checkDuplicates(char[] board) {
Set<Character> seen = new HashSet<Character>();
for (char c : board) {
if (seen.contains(c))
return false;
if(c != '.')
seen.add(c);
}
return true;
}
}