[Leetcode] Bulls and cows male cow games

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.

Credits:
Special thanks to @jeantimex for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

This problem put forward a game called Bull Cow, in fact, there are some guessing numbers on the song Star Game, there is a four-digit number, you guess a result, and then according to the results of your guess and the actual results of the comparison, indicating how many numbers and positions are correctly called bulls, It also suggests how many numbers are correct, but the wrong position is called cows, which leads us to continue to guess the correct numbers. This problem does not allow us to achieve the entire game, but only by implementing a comparison. Given two strings, let's find out a few bulls and cows. This problem requires a hash table to establish a mapping of numbers and their occurrences. The first thing I wanted to do was to use a two-pass traversal to find the same number with the same value in all locations, that is, bulls, and to record the number of occurrences of a number that is not bulls in secret. Then the second traversal of our location for guess is not bulls, if present in the hash table, cows self-increment 1, then the map value minus 1, see the following code: solution One:
classSolution { Public:    stringGethint (stringSecretstringguess) {        intm[ the] = {0}, Bulls =0, cows =0;  for(inti =0; I < secret.size (); ++i) {if(Secret[i] = = Guess[i]) + +Bulls; Else++M[secret[i]]; }         for(inti =0; I < secret.size (); ++i) {if(Secret[i]! = Guess[i] &&M[guess[i]]) {                ++cows; --M[guess[i]]; }        }        returnTo_string (Bulls) +"A"+ to_string (Cows) +"B"; }};

We can actually use a loop to get it done, when dealing with the position is not bulls, we see if the secret current position number mapping value is less than 0, it means that it appears in guess, cows self-increment 1, and then map the value plus 1, if guess the current position of the number mapping value is greater than 0, Indicates that it appears in secret, cows is increased by 1, and the map value is reduced by 1, see the code below:

Solution Two:

classSolution { Public:    stringGethint (stringSecretstringguess) {        intm[ the] = {0}, Bulls =0, cows =0;  for(inti =0; I < secret.size (); ++i) {if(Secret[i] = = Guess[i]) + +Bulls; Else {                if(M[secret[i]]++ <0) ++cows; if(m[guess[i]]-->0) ++cows; }        }        returnTo_string (Bulls) +"A"+ to_string (Cows) +"B"; }};

Finally, we can make some changes to write a little more concise, A is the value of bulls, B is the sum of bulls and cows, see the code as follows:

Solution Three:

classSolution { Public:    stringGethint (stringSecretstringguess) {        intm[ the] = {0}, A =0, B =0, i =0;  for(CharS:secret) {            Charg = guess[i++]; A+ = s = =G; b+ = (m[s]++ <0) + (m[g]-->0); }        returnTo_string (a) +"A"+ to_string (b-a) +"B"; }};

Resources:

Https://leetcode.com/discuss/67031/one-pass-java-solution

Https://leetcode.com/discuss/67125/short-c-o-n

Https://leetcode.com/discuss/67012/c-one-pass-o-n-time-o-1-space

Leetcode all in one topic summary (continuous update ...)

[Leetcode] Bulls and cows male cow games

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.