-
Notifications
You must be signed in to change notification settings - Fork 25
/
validSudoku
63 lines (62 loc) · 1.66 KB
/
validSudoku
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
class Solution {
public boolean rowCheck(char [][]board,int row,int col)
{
Set<Integer> set=new HashSet<Integer>();
for(int i=0;i<9;i++)
{
if(board[row][i]=='.')
continue;
int temp=board[row][i]-'0';
if(set.contains(temp))
return false;
set.add(temp);
}
return true;
}
public boolean colCheck(char [][]board,int row,int col)
{
Set<Integer> set=new HashSet<Integer>();
for(int i=0;i<9;i++)
{
if(board[i][col]=='.')
continue;
int temp=board[i][col]-'0';
if(set.contains(temp))
return false;
set.add(temp);
}
return true;
}
public boolean boxCheck(char [][]board,int row,int col)
{
Set<Integer> set=new HashSet<Integer>();
for(int i=row;i<row+3;i++)
{
for(int j=col;j<col+3;j++)
{
if(board[i][j]=='.')
continue;
int temp=board[i][j]-'0';
if(set.contains(temp))
return false;
set.add(temp);
}
}
return true;
}
public boolean isValid(char [][]board,int row,int col)
{
return rowCheck(board,row,col) & colCheck(board,row,col) & boxCheck(board,row-row%3,col-col%3);
}
public boolean isValidSudoku(char[][] board) {
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
if(!isValid(board,i,j))
return false;
}
}
return true;
}
}