# leetcode299 - brush the question file every day

Keywords: Algorithm leetcode Dynamic Programming

You are playing balls and cows with your friends. The rules of the game are as follows:

Write a secret number and ask your friend to guess what the number is. Every time a friend guesses, you will give him a hint containing the following information:

Guess how many digits in the number belong to the number and the exact position (called "Bulls", bull),
How many digits belong to numbers, but the position is wrong (called "Cows"). In other words, how many non bull numbers in this guess can be converted into bull numbers by rearrangement.
Give you a secret number secret and guess of your friend. Please return the hint of your friend's guess this time.

The format of the prompt is "xAyB", x is the number of bulls, y is the number of cows, A represents bulls and B represents cows.

Please note that both secret numbers and numbers guessed by friends may contain duplicate numbers.

Source: LeetCode

```package dynamic programming;

import java.util.ArrayList;
import java.util.List;

public class Demo299 {
public static void main(String[] args) {
String secret = "26094063133994602727499150667274124973793089055562273910558830696301467203272749153768965535654078449283751250857545653092968621686729409917706814537300540627739118087227655009963499397463441167702835918510438658041166926495067939893242496198497619628530255169572981407759921624409880804232541674536525805913270616308851812640586986209678002128447101252726699088841630363022078462493967634869105670015624006143712228537009471947304136502758033992179823496999111053705435937299359471650960793164211041";

String guess = "36021931621805607415778104606813995695577798566974751204914464513687009734626323432504490989551556748194593556460706444271383758798467109223783216545948588604660876714164077616398802633900341140757016437342682899611718673527210842594668599731841707797050649465438708868565632721192407759227378102576206072401073366491611139112011908973260347161762669353779405846897894481825462249914192298302531491218156408011516556466603010551290357983385464518540635403120806295744786240074001147639738202157820576";
System.out.println(getHint(secret,guess));
}
public static String getHint(String secret, String guess) {
int secretlen = secret.length();
int guesslen = guess.length();
List<Integer> listA = new ArrayList<>();
List<Integer> listB = new ArrayList<>();
int A = 0;
int B = 0;
for (int i = 0; i < secretlen; i++) {
if (secret.charAt(i)==guess.charAt(i)){
A++;
}
}
for (int i = 0; i < secretlen; i++) {
for (int j = 0; j < guesslen ; j++) {
if (!listA.contains(i)&&!listB.contains(j)&&secret.charAt(i)==guess.charAt(j)){
B++;
}
}
}
return A+"A"+B+"B";
}
}

```

Of course, the result is in line with the meaning of the question, but it timed out

So I looked at other problem solving contents, which can be directly used after one traversal
An array of 0... 10 records the numbers 0 ~ 10 respectively
As long as one of them appears responsible + + one of them appears responsible -- and when the number of + + is < 0, it means that a number can be offset
When the number of -- is > 0, it means that a 1 can be offset
The presence of guess indicates + 1. When the value recorded in the number array is less than 0, it indicates that you guessed right
The occurrence of secret indicates - 1. When the value recorded in the number array is greater than 0, it indicates that you have guessed the number correctly
Adding or subtracting one at a time records the number of current numbers respectively. Only when the actual number of guess is greater than the secret number can you answer correctly

```int secretlen = secret.length();
int guesslen = guess.length();
int A = 0;
int B = 0;
int[] nums = new int[10];
for (int i = 0; i < secretlen; i++) {
if (secret.charAt(i)==guess.charAt(i)){
A++;
}else {
if (nums[secret.charAt(i)-'0']-- > 0){
B++;
}
if (nums[guess.charAt(i)-'0']++ <0){
B++;
}
}

}
return A+"A"+B+"B";
```

Posted by stenk on Mon, 08 Nov 2021 12:41:16 -0800