Skip to content

Commit 1659815

Browse files
solves bulls and cows
1 parent 6470428 commit 1659815

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/BullsAndCows.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class BullsAndCows {
2+
public static String getHint(String secret, String guess) {
3+
int bulls = 0, cows = 0;
4+
int[] count = new int[10];
5+
for (int index =0 ; index < secret.length() ; index++) {
6+
if (secret.charAt(index) == guess.charAt(index)) {
7+
bulls++;
8+
} else {
9+
if (count[secret.charAt(index) - '0']++ < 0) cows++;
10+
if (count[guess.charAt(index) - '0']-- > 0) cows++;
11+
}
12+
}
13+
14+
return bulls + "A" + cows + "B";
15+
}
16+
}

0 commit comments

Comments
 (0)