Bulls and cows

Source: Internet
Author: User

You is 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 is in th e correct positions (called "bulls") and how many digits is in the wrong positions (called "cows"), your friend would use Those hints to find out the secret number.

For example:

Secret Number:  1807Friend ' s guess:7810

Hint: 1 Bull and 3 cows. ( 8 the bull is, the cows 0 is, and 1 7 .)

According to Wikipedia: "Bulls and Cows (also known as cows and Bulls or Pigs and Bulls or Bulls and cleots) are an old cod E-breaking mind or paper and pencil game for-or more players, predating the similar commercially marketed board game M Astermind. The numerical version of the game is usually played with 4 digits, but can also being played with 3 or any other number of Di Gits. "

Write a function to return a hint according to the secret number and friend's guess, use to A indicate the Bulls and c1/> to indicate the cows, in the above example, your function should return 1A3B .

Assume that the secret number and your friend's guess only contain digits, and their lengths is 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 <; 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";

}

Bulls and cows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.