Easy JAVA solution using bucket sort


  • 0
    Z
    public class Solution {
        public String getHint(String secret, String guess) {
            int cows = 0;
            int bulls = 0;
            int[] secret_bucket = new int[10];
            int[] guess_bucket = new int[10];
            for(int i = 0; i < secret.length(); i++){
                if(secret.charAt(i) == guess.charAt(i)) bulls++;
                else{
                    secret_bucket[secret.charAt(i) - '0']++;
                    guess_bucket[guess.charAt(i) - '0']++;
                }
            }
            for(int i = 0; i < 10; i++){
                if(secret_bucket[i] > 0 && guess_bucket[i] > 0){
                    cows += Math.min(secret_bucket[i],guess_bucket[i]);
                }
            }
            return bulls + "A" + cows + "B";
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.