Java [Leetcode 229]bulls and cows

Source: Internet
Author: User

Title Description:

You is playing the following Bulls and cows game with your friend:you write down a number and ask your friend to guess W Hat the number is. Each time your friend makes a guess, you provide a hint the indicates how many digits in said guess match your secret num ber exactly in both digit and position (called "bulls") and how many digits match the secret number but locate in the Wron G position (called "cows"). Your Friend would use successive guesses and hints to eventually derive the secret number.

For example:

Secret Number: "  1807" Friend's guess: "7810"

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

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" .

Please note that both secret number and friend's guess may contain duplicate digits, for example:

Secret number: "  1123" Friend's guess: "0111"

In this case, the 1st 1 in friend's guess is a bull, the 2nd or 3rd 1 is a cow, and your function should return .

Assume that the secret number and your friend's guess only contain digits, and their lengths is always equal.

Problem Solving Ideas:

For secret columns each time the corresponding portion is read, the Bulls variable is added one if it is the same as the part of guess, otherwise, the dictionary for the corresponding Dictionary of the secret is added one, and the dictionaries of the guess correspond to one.

Again determine if the value of the secret corresponding dictionary is less than or equal to 0, if the Bulls variable is added one (indicating that the character was already present in guess and the pairing was successful)

Determine if the value of the Guess dictionary is greater than or equal to 0, if the Bulls variable is added one (indicating that the character was already present in the previous secret and the pairing was successful)

The code is as follows:

public class Solution {public    String gethint (String secret, String guess) {        int bulls = 0, cows = 0;        Int[] count = new INT[10];        for (int i = 0; i < secret.length (); i++) {        if (Secret.charat (i) = = Guess.charat (i)) {        bulls++;        }        else {        Count[secret.charat (i)-' 0 ']++;        Count[guess.charat (i)-' 0 ']--;        if (Count[secret.charat (i)-' 0 '] <= 0)        cows++;        if (Count[guess.charat (i)-' 0 '] >= 0)        cows++;        }        }        Return bulls + "A" + cows + "B";    }}

  

Java [Leetcode 229]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.