September 08, 2019

299. Bulls and Cows

299. Bulls and Cows

class Solution {
    public String getHint(String secret, String guess) {
        
        int bulls = 0;
        int cows = 0;
        int[] countBulls = new int[10];
        int[] countCows = new int[10];
        for (int i = 0; i < secret.length(); i++) {
            int s = Character.getNumericValue(secret.charAt(i));
            int g = Character.getNumericValue(guess.charAt(i));
            if (s == g) {
                bulls ++;
            } else {
                countBulls[s] ++;
                countCows[g] ++;
            }
        }
        for (int i = 0; i < 10; i++) {
            cows += Math.min(countBulls[i], countCows[i]);
        }
        
        return Integer.toString(bulls) + "A" + Integer.toString(cows) + "B";
    }
}
comments powered by Disqus