Two run with easier logic


  • 0
    W

    First round, count bull and count non-bull digit appears count. Second round, merge to get cow.

    public class Solution {
    
    
        public String getHint(String secret, String guess) {
            int[] s = new int[10];
            int[] g = new int[10];
            int bull = 0;
            for (int i = 0; i < secret.length(); i++) {
                if (secret.charAt(i) == guess.charAt(i)) {
                    bull++;
                } else {
                    s[secret.charAt(i) - '0']++;
                    g[guess.charAt(i) - '0']++;
                }
            }
            
            int cow = 0;
            for (int i = 0; i < 10; i++) {
                cow += Math.min(s[i], g[i]);
            }
           
            return bull + "A" + cow + "B";
        }
    }
    

Log in to reply
 

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