# 题目描述（简单难度）

299、Bulls and Cows

You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called "bulls") and how many digits match the secret number but locate in the wrong position (called "cows"). Your friend will use successive guesses and hints to eventually derive the secret number.

Write a function to return a hint according to the secret number and friend's guess, use A to indicate the bulls and B to indicate the cows.

Please note that both secret number and friend's guess may contain duplicate digits.

Example 1:

Input: secret = "1807", guess = "7810"

Output: "1A3B"

Explanation: 1 bull and 3 cows. The bull is 8, the cows are 0, 1 and 7.


Example 2:

Input: secret = "1123", guess = "0111"

Output: "1A1B"

Explanation: The 1st 1 in friend's guess is a bull, the 2nd or 3rd 1 is a cow.


Note: You may assume that the secret number and your friend's guess only contain digits, and their lengths are always equal.

# 解法一

int A = 0;
for (int i = 0; i < guess.length(); i++) {
if (secret.charAt(i) == guess.charAt(i)) {
A++;
}
}


HashMap<Character, Integer> mapS = new HashMap<>();
HashMap<Character, Integer> mapG = new HashMap<>();
//统计每个数字的个数
for (int i = 0; i < secret.length(); i++) {
mapS.put(secret.charAt(i), mapS.getOrDefault(secret.charAt(i), 0) + 1);
mapG.put(guess.charAt(i), mapG.getOrDefault(guess.charAt(i), 0) + 1);
}
int B = 0;
//两者取较小
for (Character key : mapG.keySet()) {
int n1 = mapG.getOrDefault(key, 0);
int n2 = mapS.getOrDefault(key, 0);
B = B + Math.min(n1, n2);
}


public String getHint(String secret, String guess) {
int A = 0;
for (int i = 0; i < guess.length(); i++) {
if (secret.charAt(i) == guess.charAt(i)) {
A++;
}
}
HashMap<Character, Integer> mapS = new HashMap<>();
HashMap<Character, Integer> mapG = new HashMap<>();
for (int i = 0; i < secret.length(); i++) {
mapS.put(secret.charAt(i), mapS.getOrDefault(secret.charAt(i), 0) + 1);
mapG.put(guess.charAt(i), mapG.getOrDefault(guess.charAt(i), 0) + 1);
}
int B = 0;
for (Character key : mapG.keySet()) {
int n1 = mapG.getOrDefault(key, 0);
int n2 = mapS.getOrDefault(key, 0);
B = B + Math.min(n1, n2);
}
return A + "A" + (B - A) + "B";
}


public String getHint(String secret, String guess) {
int A = 0;
int[] mapS = new int[10];
int[] mapG = new int[10];
for (int i = 0; i < guess.length(); i++) {
if (secret.charAt(i) == guess.charAt(i)) {
A++;
} else {
mapS[secret.charAt(i) - '0']++;
mapG[guess.charAt(i) - '0']++;
}
}
int B = 0;
for (int i = 0; i < 10; i++) {
B += Math.min(mapS[i], mapG[i]);
}
return A + "A" + B + "B";
}


# 解法二

public String getHint(String secret, String guess) {
int bulls = 0;
int cows = 0;
int[] numbers = new int[10];
for (int i = 0; i<secret.length(); i++) {
int s = secret.charAt(i) - '0';
int g = guess.charAt(i)) - '0';
if (s == g) bulls++;
else {
//当前数小于 0, 说明之前在 guess 中出现过, 和 secret 当前的数匹配
if (numbers[s] < 0) cows++;
//当前数大于 0, 说明之前在 secret 中出现过, 和 guess 当前的数匹配
if (numbers[g] > 0) cows++;
//secret 中的数, 计数加 1
numbers[s] ++;
//guess 中的数, 计数减 1
numbers[g] --;
}
}
return bulls + "A" + cows + "B";
}


"231" 和 "321"

secret 2  3  1
guess  3  2  1
^
i

numbers[s]++, 也就是 numbers[2] = 1
numbers[g]--, 也就是 numbers[3] = -1

secret 2  3  2
guess  3  2  1
^
i

numbers[2] = 1, numbers[3] = -1

cows = 2

numbers[s]++, 也就是 numbers[3] + 1 = -1 + 1 = 0
numbers[g]--, 也就是 numbers[2] - 1 = 1 - 1 = 0

secret 2  3  2
guess  3  2  1
^
i
numbers[2] = 0, numbers[3] = 0