Class Solution

java.lang.Object
g0201_0300.s0299_bulls_and_cows.Solution

public class Solution extends Object
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 &ldquo;bulls&rdquo;, which are digits in the guess that are in the correct position.</li> <li>The number of &ldquo;cows&rdquo;, 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&rsquo;s guess <code>guess</code>, return <em>the hint for your friend&rsquo;s guess</em>.</p> <p>The hint should be formatted as <code>&quot;xAyB&quot;</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 = &ldquo;1807&rdquo;, guess = &ldquo;7810&rdquo;</p> <p><strong>Output:</strong> &ldquo;1A3B&rdquo;</p> <p><strong>Explanation:</strong></p> <pre><code> Bulls are connected with a '|' and cows are underlined: &quot;1807&quot; | &quot;7810&quot; </code></pre> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> secret = &ldquo;1123&rdquo;, guess = &ldquo;0111&rdquo;</p> <p><strong>Output:</strong> &ldquo;1A1B&rdquo;</p> <p><strong>Explanation:</strong></p> <pre><code> Bulls are connected with a '|' and cows are underlined: &quot;1123&quot; &quot;1123&quot; | or | &quot;0111&quot; &quot;0111&quot; 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 = &ldquo;1&rdquo;, guess = &ldquo;0&rdquo;</p> <p><strong>Output:</strong> &ldquo;0A0B&rdquo;</p> <p><strong>Example 4:</strong></p> <p><strong>Input:</strong> secret = &ldquo;1&rdquo;, guess = &ldquo;1&rdquo;</p> <p><strong>Output:</strong> &ldquo;1A0B&rdquo;</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 Details

    • Solution

      public Solution()
  • Method Details