My O(n) Java Solution and beats 83.76% of java submissions


  • 2
    H
    public class Solution {
    public String getHint(String secret, String guess) {
        int[] hash = new int[10];
        int bull = 0;
        for(int i=0;i<secret.length();i++){
            int secretVal = secret.charAt(i)-'0';
            int guessVal = guess.charAt(i)-'0';
            if(secretVal==guessVal)
                bull++;
            hash[secretVal]++;
            hash[guessVal]--;
        }
        int sum = 0;
        for(int i=0;i<10;i++){
            sum += Math.abs(hash[i]);
        }
        return bull+"A"+((secret.length()*2-sum)/2-bull)+"B";
    }
    

    }


Log in to reply
 

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