Java solution beats 97%. Is this DP though?


  • 0
    public class Solution {
        public String getHint(String secret, String guess) {
            if (secret.length() == 0 || guess.length() == 0) {
                return "0A0B";
            }
            int bulls = 0, cows = 0;
            char[] secretChars = secret.toCharArray();
            char[] guessChars = guess.toCharArray();
            int[][] mask = new int[2][10];
            for (int i = 0; i < secretChars.length; i += 1) {
                // check if it's a bull
                if (secretChars[i] == guessChars[i]) {
                    bulls += 1;
                } else {
                    int s = secretChars[i] - 48;
                    int g = guessChars[i] - 48;
                    mask[0][s] += 1;
                    mask[1][g] += 1;
                }
            }
            for (int i = 0; i < mask[0].length; i += 1) {
                cows += Math.min(mask[0][i], mask[1][i]);
            }
            StringBuilder sb = new StringBuilder();
            sb.append(bulls);
            sb.append("A");
            sb.append(cows);
            sb.append("B");
            return sb.toString();
        }
    }
    

Log in to reply
 

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