forked from Geekosophers/llt-daily-dsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 7 - Solution
53 lines (48 loc) · 1.52 KB
/
Day 7 - Solution
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
class Solution {
public boolean areRowsValid(char[][] board, Set<Character> set){
for(int i=0;i<9;i++){
set.clear();
for(int j=0;j<9;j++){
if(set.contains(board[i][j]))
return false;
if(board[i][j]!='.')
set.add(board[i][j]);
}
}
return true;
}
public boolean areColsValid(char[][] board, Set<Character> set){
for(int i=0;i<9;i++){
set.clear();
for(int j=0;j<9;j++){
if(set.contains(board[j][i]))
return false;
if(board[j][i]!='.')
set.add(board[j][i]);
}
}
return true;
}
public boolean areBoxesValid(char[][] board, Set<Character> set){
for(int m=0;m<9;m+=3){
for(int n=0;n<9;n+=3){
set.clear();
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(set.contains(board[i+m][j+n]))
return false;
else if(board[i+m][j+n]!='.')
set.add(board[i+m][j+n]);
}
}
}
}
return true;
}
public boolean isValidSudoku(char[][] board) {
Set<Character> set = new HashSet<>();
return areRowsValid(board, set) &&
areColsValid(board,set) &&
areBoxesValid(board,set);
}
}