Java without hash( 3 ms)


  • 7
    S
    public String getHint(String secret, String guess) {
        char[] s = secret.toCharArray();
        char[] g = guess.toCharArray();
        int a = 0, b = 0;
        int[] count = new int[10];
        for(int i = 0; i < s.length; i++){
            if(s[i] == g[i]) a++;
            else{
                if(count[s[i] - '0']++ < 0) b++;
                if(count[g[i] - '0']-- > 0) b++;
            }
        }
        return a+"A"+b+"B";
    }

Log in to reply
 

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