299. Bulls and Cows
https://leetcode.com/problems/bulls-and-cows/
js
/**
* @param {string} secret
* @param {string} guess
* @return {string}
*/
var getHint = function(secret, guess) {
var a = 0;
var b = 0;
var smap = {};
var gmap = {};
for (var i = 0; i < secret.length; i++) {
var s = secret[i];
var g = guess[i];
if (s === g) {
a += 1;
console.log(s, g, a);
} else {
if (smap[g]) {
smap[g] -= 1;
b += 1;
} else {
gmap[g] = (gmap[g] || 0) + 1;
}
if (gmap[s]) {
gmap[s] -= 1;
b += 1;
} else {
smap[s] = (smap[s] || 0) + 1;
}
}
}
return [a, 'A', b, 'B'].join('');
};
py
from collections import defaultdict
class Solution(object):
def getHint(self, secret, guess):
"""
:type secret: str
:type guess: str
:rtype: str
"""
a, b = 0, 0
smap, gmap = defaultdict(int), defaultdict(int)
for s, g in zip(secret, guess):
if s == g:
a += 1
else:
if smap[g]:
smap[g] -= 1
b += 1
else:
gmap[g] += 1
if gmap[s]:
gmap[s] -= 1
b += 1
else:
smap[s] += 1
return '%dA%dB' % (a, b)