-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path299.cpp
More file actions
24 lines (24 loc) · 714 Bytes
/
Copy path299.cpp
File metadata and controls
24 lines (24 loc) · 714 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
// Runtime: 4 ms, faster than 97.05% of C++ online submissions for Bulls and
// Cows. Memory Usage: 9 MB, less than 33.22% of C++ online submissions for
// Bulls and Cows.
public:
string getHint(string secret, string guess) {
int A = 0, B = 0;
unordered_map<char, int> m;
for (int i = 0; i < secret.size(); ++i)
if (secret[i] == guess[i])
++A;
else
++m[secret[i]];
for (int i = 0; i < secret.size(); ++i)
if (secret[i] != guess[i]) {
if (auto it = m.find(guess[i]); it != end(m)) {
++B;
if (--it->second == 0)
m.erase(it);
}
}
return to_string(A) + "A" + to_string(B) + "B";
}
};