#define MATCHED ('x')
class Solution {
public:
string getHint(string secret, string guess) {
vector<int> digitCnt(10, 0);
int bullCnt = 0;
for (int i = 0; i < secret.size(); ++i) {
if (secret[i] == guess[i]) {
++bullCnt;
guess[i] = MATCHED;
} else {
++digitCnt[secret[i] - '0'];
}
}
int cowCnt = 0;
for (int i = 0; i < guess.size(); ++i) {
if (guess[i] != MATCHED && digitCnt[guess[i] - '0'] > 0) {
// guess[i] is in secret, but not located at the
// correct position.
++cowCnt;
--digitCnt[guess[i] - '0'];
}
}
return to_string(bullCnt) + "A" + to_string(cowCnt) + "B";
}
};
secret
.
My Solution Solution 1: DFS (recursion) The Key Idea for Solving This Coding Question C++ Code /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right;
Jun 6, 2025MyCircularQueueO(k)
Apr 20, 2025O(m)
Mar 4, 2025O(n)n is the length of nums.
Feb 19, 2025or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up