4ms easy to understand JAVA solution


  • 0
    M
    public class Solution {
        int bull = 0;
        int cow = 0;
        int[] s = new int[10];
        int[] g = new int[10];
        public String getHint(String secret, String guess) {
            for(int i = 0; i < secret.length(); i++) {
                if(secret.charAt(i) == guess.charAt(i)) bull++;
            }
            for(int i = 0; i < secret.length(); i++) {
                s[secret.charAt(i) - 48]++;
                g[guess.charAt(i) - 48]++;
            }
            for(int i = 0; i < 10; i++) {
                cow += Math.min(s[i],g[i]);
            }
            cow -= bull;
            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.