Bulls and Cows

You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret number and ask your friend to guess it, each time your friend guesses a number, you give a hint, the hint tells your friend how many digits are in the correct positions (called "bulls") and how many digits are in the wrong positions (called "cows"), your friend will use those hints to find out the secret number.

For example:

Secret number:  1807
Friend‘s guess: 7810

Hint: 1 bull and 3 cows. (The bull is 8, the cows are 01 and 7.)

According to Wikipedia: "Bulls and Cows (also known as Cows and Bulls or Pigs and Bulls or Bulls and Cleots) is an old code-breaking mind or paper and pencil game for two or more players, predating the similar commercially marketed board game Mastermind. The numerical version of the game is usually played with 4 digits, but can also be played with 3 or any other number of digits."

Write a function to return a hint according to the secret number and friend‘s guess, use A to indicate the bulls and B to indicate the cows, in the above example, your function should return 1A3B.

You may assume that the secret number and your friend‘s guess only contain digits, and their lengths are always equal.

My code 1:

//Accepted

public String getHint(String secret, String guess) {

int len = secret.length();
int A = 0;
int B = 0;

char [] se = secret.toCharArray();
for(int i = 0; i < len; i++)
{
if( se[i] == guess.charAt(i))
{
A++;
se[i] = ‘ ‘;
}
}

int []mapA = new int[10];
int []mapB = new int[10];
for(int i = 0 ; i < len ; i++)
{
if( se[i] == ‘ ‘)
{
continue;
}
mapA[Integer.parseInt("" +se[i])]++;
mapB[Integer.parseInt("" +guess.charAt(i))]++;
}

for(int i = 0; i < 10; i++)
{
B += Math.min(mapA[i], mapB[i]);
}

return "" + A + "A" + B + "B";
}

My code 2:

//Accepted
public String getHintT(String secret, String guess) {

int len = secret.length();
int A = 0;
int B = 0;

char [] se = secret.toCharArray();
char [] gu = guess.toCharArray();
for(int i = 0; i < len; i++)
{
if( se[i] == gu[i])
{
A++;
se[i] = ‘ ‘;
gu[i] = ‘ ‘;
}
}
Map<Character,Integer> mapA = new HashMap<Character, Integer>();
Map<Character,Integer> mapB = new HashMap<Character, Integer>();

for(int i = 0 ; i< len; i++)
{
if( se[i] == ‘ ‘)
{
continue;
}
char key = se[i];
int value = 1;
if( mapA.containsKey(key))
{
value += mapA.get(key);
}
mapA.put(key, value);

key = gu[i];
value = 1;
if( mapB.containsKey(key))
{
value += mapB.get(key);
}
mapB.put(key, value);
}

Iterator<Character> it = mapB.keySet().iterator();

while( it.hasNext())
{
char key = it.next();
int value = mapB.get(key);
if( mapA.containsKey(key))
{
B += Math.min(value, mapA.get(key));
mapA.remove(key);
}
it.remove();
}
return "" + A + "A" + B + "B";

}

时间: 2024-10-17 13:08:48

Bulls and Cows的相关文章

[LeetCode] Bulls and Cows

Bulls and Cows You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret number and ask your friend to guess it, each time your friend guesses a number, you give a hint, the hint tells your friend how many digits

LeetCode:Bulls and Cows - 猜数字游戏

1.题目名称 Bulls and Cows(猜数字游戏) 2.题目地址 https://leetcode.com/problems/bulls-and-cows/ 3.题目内容 英文:You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret number and ask your friend to guess it, each time your friend g

Java [Leetcode 229]Bulls and Cows

题目描述: You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess ma

[LeetCode] Bulls and Cows 公母牛游戏

You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret number and ask your friend to guess it, each time your friend guesses a number, you give a hint, the hint tells your friend how many digits are in the corr

[LeetCode299]Bulls and Cows

题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess matc

Leetcode题目:Bulls and Cows

题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess matc

【Leetcode】Bulls and Cows

题目链接:https://leetcode.com/problems/bulls-and-cows/ 题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hin

Bulls and Cows leetcode

You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match yo

leetcode笔记:Bulls and Cows

一. 题目描写叙述 You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said gues