My java soluction


  • 1
    
        public String getHint(String secret, String guess) {
            if(secret== null || guess == null  || secret.length() != guess.length()){
                return "wrong input";
            }
            
            int[] number_secret = new int[10];
            int[] number_guess = new int[10];
    
            int A = 0;
            int B = 0;
            for(int i = 0; i < secret.length(); i++){
                if(secret.charAt(i) == guess.charAt(i)){
                    A++;
                }else{
                    number_secret[secret.charAt(i) -'0']++;
                    number_guess[guess.charAt(i) -'0']++;
                }
            }
            for(int i = 0; i < number_secret.length; i++){
                B += Math.min(number_secret[i], number_guess[i]);
            }
            return A+"A"+B+"B";
        }
    
    ```

Log in to reply
 

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