# Two Pointers and HashMap

• my idea is to use two pointers to count bulls , and use hashmap to count bulls + cows..

``````class Solution {
public String getHint(String secret, String guess) {
int bulls = 0;
int cowsbull = 0;
char[] schars = secret.toCharArray();
char[] gchars = guess.toCharArray();
for (int i = 0, j = 0; i < schars.length; i++, j++) {
if (schars[i] == gchars[j]) {
bulls++;
}
}
Map<Character, Integer> smap = new HashMap<>();
Map<Character, Integer> gmap = new HashMap<>();

initHash(schars, smap);
initHash(gchars, gmap);

char[] source = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

for (char c : source) {
int scount = smap.getOrDefault(c, 0);
int gcount = gmap.getOrDefault(c, 0);

int hint = scount > gcount ? gcount : scount;

cowsbull += hint;
}

int cows = cowsbull - bulls;

return bulls + "A" + cows + "B";

}
private void initHash(char[] target, Map<Character, Integer> map) {
for (int i = 0; i < target.length; i++) {
map.put(target[i], map.getOrDefault(target[i], 0) + 1);
}
}
}

``````

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