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.

**Understand the problem:**

The idea is first go through the input and get the number of bulls. Meanwhile, maintain a map and update the secret number in the map.

The second pass is to calculate the the number of cows. The method is for each character in the guess, if it is not the bull, we check if this char is in the map. If yes, we found one cow. Remember we need to remove the visited chars in the map because of the duplicated number may occur in the secret.

**Code (Java):**

public class Solution { public String getHint(String secret, String guess) { if (secret == null || secret.length() == 0 || guess == null || guess.length() == 0) { return "0A0B"; } int numBulls = 0; int numCows = 0; Map<Character, Integer> map = new HashMap<>(); // Step 1: calculate the number of bulls and update the map for (int i = 0; i < secret.length(); i++) { char s = secret.charAt(i); char g = guess.charAt(i); if (s == g) { numBulls++; } else { // Update the map if (map.containsKey(s)) { int freq = map.get(s); freq++; map.put(s, freq); } else { map.put(s, 1); } } } // Step 2: calculate the number of cows for (int i = 0; i < secret.length(); i++) { char s = secret.charAt(i); char g = guess.charAt(i); if (s != g) { if (map.containsKey(g)) { numCows++; // update the map if (map.get(g) == 1) { map.remove(g); } else { int freq = map.get(g); freq--; map.put(g, freq); } } } } return numBulls + "A" + numCows + "B"; } }

**Analysis:**

Time: O(n)

Space O(n)

**A better solution:**

Since the digits are only from 0 to 9, we don't need a map to store the digits, but just use two arrays, and count the freq for each digit.

**Code (Java):**

public class Solution { public String getHint(String secret, String guess) { if (secret == null || secret.length() == 0 || guess == null || guess.length() == 0) { return "0A0B"; } int numBulls = 0; int numCows = 0; int[] s = new int[10]; int[] g = new int[10]; // Step 1: count the number of bulls for (int i = 0; i < secret.length(); i++) { char charS = secret.charAt(i); char charG = guess.charAt(i); if (charS == charG) { numBulls++; } else { s[charS - '0']++; g[charG - '0']++; } } // Step 2: count the number of cows for (int i = 0; i < 10; i++) { numCows += Math.min(s[i], g[i]); } return numBulls + "A" + numCows + "B"; } }

## No comments:

## Post a Comment