# 14 ms, verbose JAVA solution, followed hints, using HashTables, obviously not the best, but easy to understand.

• I know there're many much better solutions out there. But just wanted to share my solution.
Any comments are welcome.

The idea is very straightforward:
Have two HashTables, key is the char that don't match in the two strings, value is the number of its occurrences in each string. Compare the two strings, if the compared chars are the same, then bulls++; if not, add this char into both HashTables; in end end, take the min of the values of the two HashTables.

``````public String getHint(String secret, String guess) {
Map<Character, Integer> secretMap = new HashMap<Character, Integer>();
Map<Character, Integer> guessMap = new HashMap<Character, Integer>();
char[] s = secret.toCharArray();
char[] g = guess.toCharArray();
int bulls = 0;
//Problem states that both strings are of equal length, so one pointer is enough
for(int i = 0; i < s.length; i++){
if(s[i] != g[i]){
if(secretMap.containsKey(s[i])){
secretMap.put(s[i], secretMap.get(s[i])+1);
} else {
secretMap.put(s[i], 1);
}
if(guessMap.containsKey(g[i])){
guessMap.put(g[i], guessMap.get(g[i])+1);
} else {
guessMap.put(g[i], 1);
}
} else {
bulls++;
}
}
int cows = 0;
//iterate thru the two maps and find out the number of cows
for(Map.Entry<Character, Integer> sEntry : secretMap.entrySet()){
for(Map.Entry<Character, Integer> gEntry : guessMap.entrySet()){
if(gEntry.getKey() == sEntry.getKey()){
cows += Math.min(gEntry.getValue(), sEntry.getValue());
}
}
}
return bulls + "A" + cows + "B";
}``````

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