Leetcode[299] Bulls and Cows
06 Dec 2015###Task1 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.
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, use A to indicate the bulls and B 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 “1A1B”. You may assume that the secret number and your friend’s guess only contain digits, and their lengths are always equal.
###Java
public class Solution {
public String getHint(String secret, String guess) {
int N = secret.length();
int countA = 0;
int countB = 0;
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
ArrayList<Character> list = new ArrayList<Character>();
for (int i = 0; i < N; i++) {
if (secret.charAt(i) == guess.charAt(i)) {
countA++;
} else {
if (map.containsKey(secret.charAt(i))) {
map.put(secret.charAt(i), map.get(secret.charAt(i)) + 1);
} else {
map.put(secret.charAt(i), 1);
}
list.add(guess.charAt(i));
}
}
for (Character c: list) {
if (map.containsKey(c) && map.get(c) > 0) {
countB++;
map.put(c, map.get(c) - 1);
}
}
return String.valueOf(countA) + "A" + String.valueOf(countB) + "B";
}
}
####Easy, contant space
public String getHint(String secret, String guess) {
int bull, cow;
bull = cow = 0;
int[] counter_secret = new int[10]; //store the digits of secrete
int[] counter_guess = new int[10]; //store the digits of guess
int L = secret.length();
for(int i=0; i<L; i++) {
counter_secret[secret.charAt(i)-'0']++;
// if meets a bull, secrete counter should not change
if(secret.charAt(i)==guess.charAt(i)) {
bull++;
counter_secret[secret.charAt(i)-'0']--;
}
else {
counter_guess[guess.charAt(i)-'0']++;
}
}
// check the two arrays item by item and sum up the cows
for(int i=0; i<=9; i++) {
cow += counter_secret[i]>=counter_guess[i] ? counter_guess[i]:counter_secret[i];
}
return bull + "A" + cow + "B";