class Solution {
public String getHint(String secret, String guess) {
int bulls = 0;
int cows = 0;
int[] secretCount = new int[10];
int[] guessCount = new int[10];
for (int i = 0; i < secret.length(); i++) {
if (secret.charAt(i) == guess.charAt(i)) {
bulls++;
} else {
secretCount[secret.charAt(i) - '0']++; // record number count
guessCount[guess.charAt(i) - '0']++;
}
}
for (int i = 0; i < 10; i++) {
cows += Math.min(secretCount[i], guessCount[i]);
}
return bulls + "A" + cows + "B";
}
}
/*
bulls correct position. x is the number of bulls
cows right number in secret, but in wrong position , can rearrange to be bulls, y is the number of cows
secret(ans), guess, return hint, xAyB
compare two String, if match bulls++
else
record in counting array, countS[i]++, countG[i]++
final
for (entire countS[]) {
cows += Math.min(countS[i], countG[i])
}
ex:
1123
b
0111
bulls = 1
others not matches, but has common 1'num in both counter
time: O(n), n is min(secret'len, 10)
space: O(1)
*/