---
# System prepended metadata

title: 0299. Bulls and Cows
tags: [Leetcode, Medium]

---

# 0299. Bulls and Cows
###### tags: `Leetcode` `Medium`
Link: https://leetcode.com/problems/bulls-and-cows/description/
## 思路
bulls只要数出有几个位置是对的就可以了
每个值的cows就是除了bulls之外每个数字在secret和guess里面的count的最小值
## Code
```java=
class Solution {
    public String getHint(String secret, String guess) {
        int[] cntS = new int[10];
        int[] cntG = new int[10];
        int n = secret.length();
        int bulls = 0;
        for(int i=0; i<n; i++){
            int a = secret.charAt(i)-'0';
            int b = guess.charAt(i)-'0';
            if(a==b) bulls++;
            else{
                cntS[a]++;
                cntG[b]++;
            }
        }
        int cows = 0;
        for(int i=0; i<10; i++){
            cows += Math.min(cntG[i], cntS[i]);
        }
        String s = String.valueOf(bulls)+'A'+String.valueOf(cows)+'B';
        return s;
    }
}
```