[LeetCode]Bulls and Cows

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

 

posted @ 2015-11-29 09:06  Weizheng_Love_Coding  阅读(113)  评论(0编辑  收藏  举报