You is playing the following Bulls and cows game with your friend:you write down a number and ask your friend to guess W Hat the number is. Each time your friend makes a guess, you provide a hint the indicates how many digits in said guess match your secret num ber exactly in both digit and position (called "bulls") and how many digits match the secret number but locate in the Wron G position (called "cows"). Your Friend would 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. ( 8
the bull is, the cows 0
is, and 1
7
.)
Write a function to return a hint according to the secret number and friend's guess, use to A
indicate the Bulls and c1/> to indicate the cows. In the above example, your function should return "1A3B"
.
Please note that both secret number and friend's guess may contain 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 should return .
Assume that the secret number and your friend's guess only contain digits, and their lengths is always equal.
Background introduction:
Usually played by two people, one side out of numbers, the other guessing. The number of people to think of a good one without repeating the number of 4 numbers, can not let the guessing people know. Guess the person can start to guess. Each guess a number, the number of people to give a few a a few B according to this figure, where a before the number indicates the correct number of positions, and B before the number of the number is correct and the number of incorrect position. If the correct answer is 5234, and guess the person guess 5346, then is 1A2B, which has a 5 of the position of the pair, recorded as 1 A, and 3 and 4 of the two number pairs, and the position is not correct, so the record is 2B, together is 1a2b. The next guessing person continues to guess according to the number of a few b of the subject, until the Guess (ie, 4a0b). Code:package Leetcode;
Import java.util.*;
public class Bullsandcows {
public static string Gethint (String secret, String guess) {
Own ideas: To do with a set, consider not comprehensive, not from the problem
/*
* int bulls = 0; int cows = 0; list<character> arr = new arraylist<> ();
* FOR (int i=0;i<secret.length (); i++) {Arr.add (Secret.charat (i));}
* FOR (int i=0;i<secret.length (); i++) {if (Secret.charat (i) = =
* Guess.charat (i)) {bulls + +; Arr.set (i, ' a ');} Else
* IF (Arr.contains (Guess.charat (i))) {cows + +;}} if (index ==4) return
* "4A0B"; return bulls+ "A" +cows+ "B"; } public static void Main (string[]
* args) {//TODO auto-generated method stub String A = "01"; String b
* = "11"; System.out.print (Gethint (A, b));
*/
Idea: Secret has one char, result plus; Guess has one, result reduce.
This is used with an array of records, +1,-1;
/*
* IF (secret.length () = = 0) {return "0a0b";}
*
* int bull = 0; int cow = 0; int [] result = new int [10];
*
* for (int i = 0;i<secret.length (); i++) {int x = Secret.charat (i)-48; Char converted to INT type 1
* Int y = Guess.charat (i)-48;
*
* if (x = = y) {bull++;} else {if (Result[x] < 0) {cow++;} result[x]++;
* IF (Result[y] > 0) {cow++;} result[y]--; } }
*
* Return bull+ "A" +cow+ "B";
*/
The simplest idea: Record the number of occurrences of each digit on the array!! Good idea!!
If the corresponding equality is not recorded, the inequality corresponds to the array of + 1, the last to take the corresponding position of the two arrays of the smaller values;//This is the number repeated with the array record, if repeated .... If you do not repeat ....
int len = Secret.length ();
int[] Secretarr = new INT[10];
int[] Guessarr = new INT[10];
int bull = 0, cow = 0;
for (int i = 0; i < len; ++i) {
if (Secret.charat (i) = = Guess.charat (i)) {
++bull;
} else {
++secretarr[secret.charat (i)-' 0 ']; Char converted to int type 2
++guessarr[guess.charat (i)-' 0 '];
}
}
for (int i = 0; i < ++i) {
Cow + = Math.min (Secretarr[i], guessarr[i]);
}
Return "" + Bull + "A" + Cow + "B";
}
}
Thinking: How to do it with hash?
Leetcode-bulls and cows