-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBullsAndCows.java
More file actions
32 lines (27 loc) · 839 Bytes
/
BullsAndCows.java
File metadata and controls
32 lines (27 loc) · 839 Bytes
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
public class BullsAndCows {
public String getHint(String secret, String guess) {
int a = 0;
int b;
int cSum = 0;
int[] counts = new int[10];
for(int i = 0; i < secret.length(); i++) {
if(secret.charAt(i) == guess.charAt(i)) a++;
counts[secret.charAt(i) - '0']++;
counts[guess.charAt(i) - '0']--;
}
for(int count: counts) {
System.out.println(count);
if(count > 0)
cSum += count;
}
b = secret.length() - cSum - a;
System.out.println(cSum);
String res = a + "A" + b + "B";
return res;
}
public static void main(String[] args) {
BullsAndCows a = new BullsAndCows();
System.out.println(a.getHint("1122",
"3456"));
}
}