|
9 | 9 |
|
10 | 10 | [Next >](../longest-increasing-subsequence "Longest Increasing Subsequence")
|
11 | 11 |
|
12 | | -## [299. Bulls and Cows (Easy)](https://leetcode.com/problems/bulls-and-cows "猜数字游戏") |
| 12 | +## [299. Bulls and Cows (Medium)](https://leetcode.com/problems/bulls-and-cows "猜数字游戏") |
13 | 13 |
|
14 | | -<p>You are playing the following <a href="https://en.wikipedia.org/wiki/Bulls_and_Cows" target="_blank">Bulls and Cows</a> 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 your secret number exactly in both digit and position (called "bulls") and how many digits match the secret number but locate in the wrong position (called "cows"). Your friend will use successive guesses and hints to eventually derive the secret number.</p> |
| 14 | +<p>You are playing the <strong><a href="https://en.wikipedia.org/wiki/Bulls_and_Cows" target="_blank">Bulls and Cows</a></strong> game with your friend.</p> |
15 | 15 |
|
16 | | -<p>Write a function to return a hint according to the secret number and friend's guess, use <code>A</code> to indicate the bulls and <code>B</code> to indicate the cows. </p> |
| 16 | +<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> |
17 | 17 |
|
18 | | -<p>Please note that both secret number and friend's guess may contain duplicate digits.</p> |
| 18 | +<ul> |
| 19 | + <li>The number of "bulls", which are digits in the guess that are in the correct position.</li> |
| 20 | + <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> |
| 21 | +</ul> |
19 | 22 |
|
| 23 | +<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> |
| 24 | + |
| 25 | +<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> |
| 26 | + |
| 27 | +<p> </p> |
20 | 28 | <p><strong>Example 1:</strong></p>
|
21 | 29 |
|
22 | 30 | <pre>
|
23 | 31 | <strong>Input:</strong> secret = "1807", guess = "7810"
|
24 | | - |
25 | 32 | <strong>Output:</strong> "1A3B"
|
26 | | - |
27 | | -<strong>Explanation:</strong> <code>1</code><span style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";"> bull and </span><code>3</code><span style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";"> cows. The bull is </span><code>8</code><span style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";">, the cows are </span><code>0</code><span style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";">, </span><code>1</code><span style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";"> and </span><code>7<font face="sans-serif, Arial, Verdana, Trebuchet MS">.</font></code></pre> |
| 33 | +<strong>Explanation:</strong> Bulls are connected with a '|' and cows are underlined: |
| 34 | +"1807" |
| 35 | + | |
| 36 | +"<u>7</u>8<u>10</u>"</pre> |
28 | 37 |
|
29 | 38 | <p><strong>Example 2:</strong></p>
|
30 | 39 |
|
31 | 40 | <pre>
|
32 | 41 | <strong>Input:</strong> secret = "1123", guess = "0111"
|
33 | | - |
34 | 42 | <strong>Output:</strong> "1A1B"
|
| 43 | +<strong>Explanation:</strong> Bulls are connected with a '|' and cows are underlined: |
| 44 | +"1123" "1123" |
| 45 | + | or | |
| 46 | +"01<u>1</u>1" "011<u>1</u>" |
| 47 | +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. |
| 48 | +</pre> |
| 49 | + |
| 50 | +<p><strong>Example 3:</strong></p> |
| 51 | + |
| 52 | +<pre> |
| 53 | +<strong>Input:</strong> secret = "1", guess = "0" |
| 54 | +<strong>Output:</strong> "0A0B" |
| 55 | +</pre> |
| 56 | + |
| 57 | +<p><strong>Example 4:</strong></p> |
| 58 | + |
| 59 | +<pre> |
| 60 | +<strong>Input:</strong> secret = "1", guess = "1" |
| 61 | +<strong>Output:</strong> "1A0B" |
| 62 | +</pre> |
35 | 63 |
|
36 | | -<strong>Explanation: </strong>The 1st <code>1 </code><span style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";">in friend's guess is a bull, the 2nd or 3rd </span><code>1</code><span style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";"> is a cow</span><span style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";">.</span></pre> |
| 64 | +<p> </p> |
| 65 | +<p><strong>Constraints:</strong></p> |
37 | 66 |
|
38 | | -<p><strong>Note: </strong>You may assume that the secret number and your friend's guess only contain digits, and their lengths are always equal.</p> |
| 67 | +<ul> |
| 68 | + <li><code>1 <= secret.length, guess.length <= 1000</code></li> |
| 69 | + <li><code>secret.length == guess.length</code></li> |
| 70 | + <li><code>secret</code> and <code>guess</code> consist of digits only.</li> |
| 71 | +</ul> |
39 | 72 |
|
40 | 73 | ### Related Topics
|
41 | 74 | [[Hash Table](../../tag/hash-table/README.md)]
|
0 commit comments