Created
September 10, 2020 07:11
-
-
Save RP-3/7dade4d66c480b16bcdc0173531f458e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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