🎨Now live: Try our Free AI Image Generation Feature

Description
The problem involves a game where you are given two strings: secret
and guess
, both of the same length. You need to provide a hint that includes:
- The number of "bulls" (correct digits in the correct positions).
- The number of "cows" (correct digits but in the wrong positions).
The output should be a string formatted as "xAyB"
, where x
is the number of bulls and y
is the number of cows.
Intuition
To solve this problem, we need to distinguish between two scenarios:
- A digit in
guess
exactly matches the corresponding digit insecret
(a bull). - A digit in
guess
is insecret
but in the wrong position (a cow).
By counting bulls first and then handling cows based on the frequency of unmatched digits, we can efficiently compute the desired hint.
Approach
- Initialize counters: Start with
bulls = 0
,cows = 0
, and an arrayraj
of size 10 to keep track of the frequency of digits that have been encountered but not matched as bulls. - Iterate through the strings: For each character in the strings:
- If the digits in
secret
andguess
match, increment the bulls counter. - If they don't match, update theraj
array: - If the current digit insecret
was previously encountered inguess
, it means we have found a cow, so increment the cows counter. - If the current digit inguess
was previously encountered insecret
, increment the cows counter. - Update theraj
array to reflect the current unmatched digits. - Return the result: Format the result as
"xAyB"
wherex
is the number of bulls andy
is the number of cows.
Complexity
Time Complexity:
O(n)
, where n
is the length of the strings secret
and guess
. We iterate through the strings once, making the solution linear in time.
Space Complexity:
O(1)
. The raj
array is of fixed size (10), and no additional space is used that scales with the input size.
Code
C++
class Solution {
public:
string getHint(string secret, string guess) {
int bulls = 0;
int cows = 0;
vector raj(10, 0);
for (int i = 0; i < secret.length(); i++) {
int sec = secret[i] - '0';
int gu = guess[i] - '0';
if (sec == gu) {
bulls++;
continue;
}
if (raj[sec] < 0) {
cows++;
}
if (raj[gu] > 0) {
cows++;
}
raj[sec]++;
raj[gu]--;
}
return to_string(bulls) + "A" + to_string(cows) + "B";
}
};
Python
class Solution:
def getHint(self, secret: str, guess: str) -> str:
bulls = 0
cows = 0
raj = [0] * 10
for i in range(len(secret)):
sec = int(secret[i])
gu = int(guess[i])
if sec == gu:
bulls += 1
continue
if raj[sec] < 0:
cows += 1
if raj[gu] > 0:
cows += 1
raj[sec] += 1
raj[gu] -= 1
return f"{bulls}A{cows}B"
Java
class Solution {
public String getHint(String secret, String guess) {
int bulls = 0;
int cows = 0;
int[] raj = new int[10];
for (int i = 0; i < secret.length(); i++) {
int sec = Character.getNumericValue(secret.charAt(i));
int gu = Character.getNumericValue(guess.charAt(i));
if (sec == gu) {
bulls++;
continue;
}
if (raj[sec] < 0) {
cows++;
}
if (raj[gu] > 0) {
cows++;
}
raj[sec]++;
raj[gu]--;
}
return bulls + "A" + cows + "B";
}
}
JavaScript
/**
* @param {string} sec
* @param {string} guess
* @return {string}
*/
var getHint = function (sec, guess) {
let bulls = 0;
let cows = 0;
let raj = Array(10).fill(0);
for (let i = 0; i < sec.length; i++) {
let gu = parseInt(guess[i]);
let secret = parseInt(sec[i]);
if (secret === gu) {
bulls++;
continue;
}
if (raj[secret] < 0) {
cows++;
}
if (raj[gu] > 0) {
cows++;
}
raj[secret]++;
raj[gu]--;
}
return (`${bulls}A${cows}B`);
};