forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidSudoku.java
More file actions
103 lines (82 loc) · 2.33 KB
/
ValidSudoku.java
File metadata and controls
103 lines (82 loc) · 2.33 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package Hashing;
import java.util.*;
/**
* Author - archit.s
* Date - 28/10/18
* Time - 7:03 PM
*/
public class ValidSudoku {
public boolean checkRowWise(List<String> A){
for(int i=0;i<A.size();i++){
String row = A.get(i);
Set<Character> s = new HashSet<>();
for(int j=0;j<row.length();j++){
Character temp = row.charAt(j);
if(temp == '.'){
continue;
}
else if(s.contains(temp)){
return true;
}
else{
s.add(temp);
}
}
}
return false;
}
public boolean checkColWise(List<String> A){
int colL = A.get(0).length();
int rowL = A.size();
for(int i=0;i<colL;i++){
Set<Character> s = new HashSet<>();
for(int j=0;j<rowL;j++){
Character c = A.get(j).charAt(i);
if(c == '.'){
continue;
}
if(s.contains(c)){
return true;
}
else{
s.add(c);
}
}
}
return false;
}
public boolean checkBoxWise(List<String> A){
int colL = A.get(0).length();
int rowL = A.size();
int boxCount = (colL/3)*(rowL/3);
Map<Integer,Set<Character>> map = new HashMap<>();
for(int i=0;i<boxCount;i++){
map.put(i,new HashSet<>());
}
for(int i=0;i<A.size();i++){
String row = A.get(i);
for(int j=0;j<row.length();j++){
int boxNumber = (j/3)*(colL/3) + (i/3);
Set<Character> s = map.get(boxNumber);
Character temp = row.charAt(j);
if(temp == '.'){
continue;
}
else if(s.contains(temp)){
return true;
}
else{
s.add(temp);
map.put(boxNumber,s);
}
}
}
return false;
}
public int isValidSudoku(final List<String> A) {
if(checkRowWise(A) || checkColWise(A) || checkBoxWise(A) ){
return 0;
}
return 1;
}
}