Skip to content

Instantly share code, notes, and snippets.

@RP-3
Created September 10, 2020 07:11
Show Gist options
  • Save RP-3/7dade4d66c480b16bcdc0173531f458e to your computer and use it in GitHub Desktop.
Save RP-3/7dade4d66c480b16bcdc0173531f458e to your computer and use it in GitHub Desktop.
/**
* @param {string} secret
* @param {string} guess
* @return {string}
*/
var getHint = function(secret, guess) {
let [bulls, cows] = [0, 0];
const secretDigits = new Array(10).fill(0);
for(let i=0; i<secret.length; i++){
secretDigits[secret[i]]++;
}
for(let i=0; i<guess.length; i++){
if(secret[i] === guess[i]){
bulls++;
secretDigits[secret[i]]--;
}
}
for(let i=0; i<guess.length; i++){
if(secret[i] !== guess[i] && secretDigits[guess[i]]){
cows++;
secretDigits[guess[i]]--;
}
}
return `${bulls}A${cows}B`;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment