Skip to content

Commit 2bb2052

Browse files
authored
Update BullsAndCows.java
1 parent d5d29c9 commit 2bb2052

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
1+
class BullsAndCows {
2+
public String getHint(String secret, String guess) {
3+
int A = 0;
4+
int B = 0;
5+
int[] count1 = new int[10];
6+
int[] count2 = new int[10];
17

8+
for (int i = 0; i < secret.length(); ++i)
9+
if (secret.charAt(i) == guess.charAt(i))
10+
++A;
11+
else {
12+
++count1[secret.charAt(i) - '0'];
13+
++count2[guess.charAt(i) - '0'];
14+
}
15+
16+
for (int i = 0; i < 10; ++i)
17+
B += Math.min(count1[i], count2[i]);
18+
19+
return String.valueOf(A) + "A" + String.valueOf(B) + "B";
20+
}
21+
}

0 commit comments

Comments
 (0)