Question

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.

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.

Please note that both secret number and friend’s guess may contain duplicate digits.

Example 1:

Input: secret = "1807", guess = "7810"

Output: "1A3B"

Explanation: 1 bull and 3 cows. The bull is 8, the cows are 0, 1 and 7.

Example 2:

Input: secret = "1123", guess = "0111"

Output: "1A1B"

Explanation: The 1st 1 in friend's guess is a bull, the 2nd or 3rd 1 is a cow.

Thinking:

  • Method 1: 通过数组代替哈希表。
class Solution {
    public String getHint(String secret, String guess) {
        StringBuilder sb = new StringBuilder();
        if(secret == null || guess == null) return sb.toString();
        int len = Math.min(secret.length(), guess.length());
        int countA = 0;
        int[] sTable = new int[10];
        int[] gTable = new int[10];
        int i = 0;
        for(; i < len; i++){
            char s = secret.charAt(i);
            char g = guess.charAt(i);
            if(s == g)
                countA++;
            else{
                sTable[s - '0']++;
                gTable[g - '0']++;
            }
        }
        sb.append(countA);
        sb.append("A");
        String temp = secret.length() > guess.length() ? secret: guess;
        int[] tempTable = secret.length() > guess.length() ? sTable: gTable;
        int longLen = temp.length();
        for(; i < longLen; i++)
            tempTable[temp.charAt(i) - '0']++;
        int countB = 0;
        for(i = 0; i < 10; i++){
            countB += Math.min(sTable[i], gTable[i]);
        }
        sb.append(countB);
        sb.append("B");
        return sb.toString();
    }
}

Second time

  1. We actually need 2 arrays to save the frequency of the each letters.
  2. If save position and value, nothing saved to table, and A++.
  3. If not save, increase the frequency for both of the values.
  4. Finally we take the sum of the minimum values in bonth arrays, which is the value of B.
    class Solution {
     public String getHint(String secret, String guess) {
         char[] sArr = secret.toCharArray();
         char[] gArr = guess.toCharArray();
         int A = 0;
         int B = 0;
         int[] aCount = new int[10];
         int[] bCount = new int[10];
         for(int i = 0; i < sArr.length; i++){
             if(sArr[i] == gArr[i]){
                 A++;
             }else{
                 aCount[sArr[i] - '0']++;
                 bCount[gArr[i] - '0']++;
             }
         }
         for(int i = 0; i < 10; i++){
             B += Math.min(aCount[i], bCount[i]);
         }
         return A + "A" + B + "B";
     }
    }