import java.util.*;
public class Solution299 {
public static String getHint(String secret, String guess)
{
char[] se = secret.toCharArray();
char[] gu = guess.toCharArray();
int len = se.length;
int bull = 0;
int cow = 0;
Map<Character, Integer> seHas = new HashMap<haracter, Integer>();
for (int i = 0; i < len; i++) {
if (!seHas.containsKey(se[i])) {
seHas.put(se[i], 1);
} else {
seHas.put(se[i], seHas.get(se[i]) + 1);
}
}
Boolean[] b = new Boolean[len];
for (int i = 0; i < len; i++) {
if (se[i] == gu[i]) {
b[i] = true;
seHas.put(gu[i], seHas.get(gu[i])-1);
cow++;
}else {
b[i] = false;
}
}
for (int j = 0; j < len; j++) {
if(b[j] == false && seHas.get(gu[j])!=null && seHas.get(gu[j])>0) {
bull ++;
seHas.put(gu[j], seHas.get(gu[j])-1);
}
}
return cow + "A" + bull + "B";
}
public static void main(String[]args)
{
System.out.println(getHint("1807","7810"));
}
}
Bulls and Cows 带测试版
最新推荐文章于 2019-10-12 17:26:14 发布