My AC Java Solution


  • 0
    D
            int bulls = 0;
            int cows = 0;
            int[] numbers = new int[10];
            for (int i = 0; i<secret.length(); i++) {
                if (secret.charAt(i) == guess.charAt(i)){
                    bulls++;
                } 
                else {
                    numbers[secret.charAt(i) - '0'] ++;
                }
            }
            
            for (int i = 0; i<secret.length(); i++) {
                if (secret.charAt(i) != guess.charAt(i) && numbers[guess.charAt(i) - '0'] > 0) {
                    cows++;
                    numbers[guess.charAt(i) - '0']--;
                }
            }
            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.