leetcode 299猜数字

class Solution {
    public String getHint(String secret, String guess) {
        int[] secretCountMap = new int[10], guessCountMap = new int[10];
        int a = 0, b = 0;
        char[] sChars = secret.toCharArray(), gChars = guess.toCharArray();
        for (int i = 0; i < sChars.length; i++) {
            if (sChars[i] == gChars[i]) a++;
            else {
                secretCountMap[sChars[i] - '0']++;
                guessCountMap[gChars[i] - '0']++;
            }
        }
        for (int i = 0; i <= 9; i++) {
            b += Math.min(secretCountMap[i], guessCountMap[i]);
        }
        return a+"A"+b+"B";
    }
}

你可能感兴趣的:(java,leetcode,算法,职场和发展)