Package g0201_0300.s0299_bulls_and_cows
Class Solution
java.lang.Object
g0201_0300.s0299_bulls_and_cows.Solution
299 - Bulls and Cows.<p>Medium</p>
<p>You are playing the <strong><a href="https://en.wikipedia.org/wiki/Bulls_and_Cows" target="_top">Bulls and Cows</a></strong> game with your friend.</p>
<p>You write down a secret number and ask your friend to guess what the number is. When your friend makes a guess, you provide a hint with the following info:</p>
<ul>
<li>The number of “bulls”, which are digits in the guess that are in the correct position.</li>
<li>The number of “cows”, which are digits in the guess that are in your secret number but are located in the wrong position. Specifically, the non-bull digits in the guess that could be rearranged such that they become bulls.</li>
</ul>
<p>Given the secret number <code>secret</code> and your friend’s guess <code>guess</code>, return <em>the hint for your friend’s guess</em>.</p>
<p>The hint should be formatted as <code>"xAyB"</code>, where <code>x</code> is the number of bulls and <code>y</code> is the number of cows. Note that both <code>secret</code> and <code>guess</code> may contain duplicate digits.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> secret = “1807”, guess = “7810”</p>
<p><strong>Output:</strong> “1A3B”</p>
<p><strong>Explanation:</strong></p>
<pre><code> Bulls are connected with a '|' and cows are underlined:
"1807"
|
"7810"
</code></pre>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> secret = “1123”, guess = “0111”</p>
<p><strong>Output:</strong> “1A1B”</p>
<p><strong>Explanation:</strong></p>
<pre><code> Bulls are connected with a '|' and cows are underlined:
"1123" "1123"
| or |
"0111" "0111"
Note that only one of the two unmatched 1s is counted as a cow since the non-bull digits can only be rearranged to allow one 1 to be a bull.
</code></pre>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> secret = “1”, guess = “0”</p>
<p><strong>Output:</strong> “0A0B”</p>
<p><strong>Example 4:</strong></p>
<p><strong>Input:</strong> secret = “1”, guess = “1”</p>
<p><strong>Output:</strong> “1A0B”</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= secret.length, guess.length <= 1000</code></li>
<li><code>secret.length == guess.length</code></li>
<li><code>secret</code> and <code>guess</code> consist of digits only.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
getHint
-