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.

For example:

Secret number:  "1807"
Friend's guess: "7810"

Hint:

1bull and3cows. (The bull is8, the cows are0,1and7.)

Write a function to return a hint according to the secret number and friend's guess, useAto indicate the bulls andBto 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 1st1in friend's guess is a bull, the 2nd or 3rd1is 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.

Solution:

Method1 :

Using hash map to keep track of each character in secret number and its appearance times. If the character appear at the same position are the same, update bull; else, update the hash map, and cow.

public String getHint(String secret, String guess) {
        Map<Character, Integer> hm = new HashMap<Character, Integer>();
        int countBull = 0;
        int countCow = 0;

        for(int i = 0; i < secret.length(); i++){
            char s = secret.charAt(i);
            char g = guess.charAt(i);
            if(s==g){
                countBull++;
            }else{
                if(hm.containsKey(s)){
                    int freq = hm.get(s) + 1;
                    hm.put(s, freq);
                }else{
                    hm.put(s,1);
                }
            }
        }

        for(int i = 0; i < secret.length(); i++){
            char s = secret.charAt(i);
            char g = guess.charAt(i);
            if(s!=g){
                if(hm.containsKey(g)){
                    countCow++;
                    int freq = hm.get(g) - 1;
                    if (freq == 0){
                        hm.remove(g);
                    }else{
                        hm.put(g, freq);
                    }
                }
            }
        }
        return countBull + "A" + countCow + "B";

    }

Method 2:

Using an array with size 10 to store the appearance time of '0' - '9'. For array [i]:

  • if = 0, means times of i appeared in secret == times of i appeared in guess
  • if > 0, means times of i appeared in secret > times of i appeared in guess
    • if i appeared in guess now, cow++
  • if < 0, means times of i appeared in guess < times of i appeared in secret
    • if i appeared in secret now, cow++
  • if i appeared in secret, array[i] ++
  • if i appeared in guess, array[i] --
public String getHint(String secret, String guess) {
        int[] digit = new int[10];
        int countCow = 0;
        int countBull = 0;
        for(int i = 0; i < secret.length(); i++){
            char s = secret.charAt(i);
            char g = guess.charAt(i);
            if(s == g){
                countBull ++;
            }else{
                if(digit[ s -'0'] < 0){
                    countCow ++;
                }
                if(digit[g - '0'] > 0){
                    countCow++;
                }
                digit[s-'0'] ++;
                digit[g-'0'] --;
            }
        }
         return countBull + "A" + countCow + "B";

 }

results matching ""

    No results matching ""