-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
299.cpp
54 lines (51 loc) · 1.55 KB
/
299.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
__________________________________________________________________________________________________
sample 4 ms submission
class Solution {
public:
string getHint(string secret, string guess) {
if (secret.size() != guess.size())
return "0A0B";
int a = 0;
int b = 0;
vector<pair<int, int>> v(10, {0, 0});
for (int i = 0; i < guess.size(); i++)
{
if (secret[i] == guess[i])
a++;
else
{
v[guess[i] - '0'].first++;
v[secret[i] - '0'].second++;
}
}
for (pair<int, int> c : v)
{
b += min(c.first, c.second);
}
return to_string(a) + 'A' + to_string(b) + 'B';
}
};
__________________________________________________________________________________________________
sample 8492 kb submission
class Solution {
public:
string getHint(string secret, string guess) {
vector<int> f(10, 0);
vector<int> g(10, 0);
int bulls = 0, cows = 0;
for(int i = 0; i < secret.size(); i++)
{
if(secret[i] == guess[i])
bulls++;
else
{
f[secret[i] - '0']++;
g[guess[i] - '0']++;
}
}
for(int i = 0; i < 10; i++)
cows += min(f[i], g[i]);
return to_string(bulls) + "A" + to_string(cows) + "B";
}
};
__________________________________________________________________________________________________