Skip to content

Instantly share code, notes, and snippets.

@riyafa
Created October 9, 2020 03:10
Show Gist options
  • Save riyafa/873b61a1c83fe7c6eea68fb6b16122fb to your computer and use it in GitHub Desktop.
Save riyafa/873b61a1c83fe7c6eea68fb6b16122fb to your computer and use it in GitHub Desktop.
Solution for LeetCode Bulls and Cows
public class BullsAndCows {
public String getHint(String secret, String guess) {
int n = secret.length();
int[] map = new int[10];
int a = 0;
int b = 0;
for (int i = 0; i < n; i++) {
int s = secret.charAt(i) - '0';
int g = guess.charAt(i) - '0';
if (s == g) {
a++;
} else {
if (map[s] < 0) {
b++;
}
if (map[g] > 0) {
b++;
}
map[s]++;
map[g]--;
}
}
return a + "A" + b + "B";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment