299. 猜数字游戏
class Solution:
def getHint(self, secret: str, guess: str) -> str:
x, y = 0, 0
cntS, cntG = [0] * 10, [0] * 10
for s, g in zip(secret, guess):
if s == g:
x += 1
else:
cntS[int(s)] += 1
cntG[int(g)] += 1
y = sum(min(s, g) for s, g in zip(cntS, cntG))
return f'{x}A{y}B'

class Solution {
public:
string getHint(string secret, string guess) {
int x = 0;
vector<int> cntS(10), cntG(10);
for (int i = 0; i < secret.size(); ++i){
if (secret[i] == guess[i]){
++x;
}else{
++cntS[secret[i] - '0'];
++cntG[guess[i] - '0'];
}
}
int y = 0;
for (int i = 0; i < 10; ++i){
y += min(cntS[i], cntG[i]);
}
return to_string(x) + "A" + to_string(y) + "B";
}
};