Problem: 299. 猜数字游戏
思路
👨🏫 灵神
复杂度
Code
class Solution {
public String getHint(String secret, String guess) {
int a = 0;
int[] cntS = new int[10];
int[] cntG = new int[10];
for(int i = 0; i < secret.length(); i++)
{
if(secret.charAt(i) == guess.charAt(i))
a++;
else
{
cntS[secret.charAt(i) - '0']++;
cntG[guess.charAt(i) - '0']++;
}
}
int b = 0;
for(int i = 0; i < 10; i++)
b += Math.min(cntS[i],cntG[i]);
return a + "A" + b + "B";
}
}