Very few javascript solutions, here's one. Pls leave feedback. Tks.


  • 0
    R
    /**
     * @param {string} secret
     * @param {string} guess
     * @return {string}
     */
    var getHint = function(secret, guess) {
        var secret_digits={},
            guess_digits=[],
            s = secret.split(''),
            g = guess.split(''),
            bulls = 0,
            cows = 0;
        for(var i=0; i<s.length; i++){
            if (s[i]===g[i]){
                bulls++;
            }
            else {
                secret_digits[s[i]] = (secret_digits[s[i]] === undefined)? 1 : secret_digits[s[i]]+1;
                guess_digits.push(g[i]);
            }
        }
    
        guess_digits.forEach(function(digit) {
            if(secret_digits[digit]){
                cows++;
                secret_digits[digit]--;
            }
        });
        return bulls + 'A' + cows + 'B';
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.