Very Simple JAVA code


  • 2
    F
    public String getHint(String secret, String guess) {
        int[] a = new int[10];
        int[] b = new int[10];
        int A = 0, B = 0;
        for (int i = 0; i < secret.length(); i++) {
            if (secret.charAt(i) == guess.charAt(i)) {
                A++;
            } else {
                a[secret.charAt(i) - '0']++;
                b[guess.charAt(i) - '0']++;
            }
        }
        for (int i = 0; i < 10; i++) {
            B += Math.min(a[i], b[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.