Lettcode_299_Bulls and Cows
You are playing the followingBulls and Cowsgame 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 to adjust 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.
For example:
Secret number: "1807"Friend's guess: "7810"
Hint:
1
Bull and
3
Cows. (The bull is
8
, The cows are
0
,
1
And
7
.)
Write a function to return a hint according to the secret number and friend's guess, useA
To indicate the bulls andB
To indicate the cows. In the above example, your function shocould return"1A3B"
.
Please note that both secret number and friend's guess may contain in duplicate digits, for example:
Secret number: "1123"Friend's guess: "0111"
In this case, the 1st
1
In friend's guess is a bull, the 2nd or 3rd
1
Is a cow, and your function shoshould return
"1A1B"
.
You may assume that the secret number and your friend's guess only contain digits, and their lengths are always equal
Ideas:
(1) The question is to give two strings of numbers, one of which can be viewed as a password, and the other can be viewed as a number to be guessed, record the number (with the same location and number) with the correct number as number + A. record the number that is not in the correct position and included in the password as number + B.
(2) This question mainly deals with character matching. Because the numbers in the two strings are the same, the bull number can be obtained only once if the positions are the same and the numbers are the same; if the location is different and the number is the same, special processing is required to obtain the number of cows. On the one hand, a certain number may appear multiple times in a certain string; on the other hand, the same location and the same number also need to be filtered. Create a Map for storage and filtering. The key of the map is the number of strings, and the value is the number of occurrences of the number. In this way, you can store the number and its number in one traversal, and then perform two traversal to obtain the number of identical numbers at the same location and the number of identical numbers at different locations, respectively, that is, the request. For details, see the code below.
(3) I hope this article will help you. Thank you.
The algorithm code is implemented as follows:
import java.util.HashMap;import java.util.Map;public class Bulls_and_Cows {public static void main(String[] args) {System.err.println(getHint("1122", "1222"));}public static String getHint(String secret, String guess) {char[] se = secret.toCharArray();char[] gu = guess.toCharArray();int len = se.length;int bull = 0;int cow = 0;Map<character, integer=""> seHas = new HashMap<character, integer="">();for (int i = 0; i < len; i++) {if (!seHas.containsKey(se[i])) {seHas.put(se[i], 1);} else {seHas.put(se[i], seHas.get(se[i]) + 1);}}Boolean[] b = new Boolean[len];for (int i = 0; i < len; i++) {if (se[i] == gu[i]) {b[i] = true;seHas.put(gu[i], seHas.get(gu[i])-1);cow++;}else {b[i] = false;}}for (int j = 0; j < len; j++) {if(b[j] == false && seHas.get(gu[j])!=null && seHas.get(gu[j])>0) {bull ++;seHas.put(gu[j], seHas.get(gu[j])-1);}}return cow + "A" + bull + "B";}}</character,></character,>