-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0791_customSortString.cc
64 lines (58 loc) · 1.08 KB
/
Problem_0791_customSortString.cc
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
55
56
57
58
59
60
61
62
63
64
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
string customSortString1(string order, string s)
{
int n = order.length();
int m = s.length();
int cnt[26] = {0};
string ans;
for (int i = 0; i < m; i++)
{
cnt[s[i] - 'a']++;
}
for (int i = 0; i < n; i++)
{
int count = cnt[order[i] - 'a'];
if (count > 0)
{
ans += string(count, order[i]);
cnt[order[i] - 'a'] = 0;
}
}
for (int i = 0; i < 26; i++)
{
if (cnt[i] > 0)
{
ans += string(cnt[i], i + 'a');
}
}
return ans;
}
string customSortString2(string order, string s)
{
vector<int> val(26);
for (int i = 0; i < order.size(); ++i)
{
val[order[i] - 'a'] = i + 1;
}
std::sort(s.begin(), s.end(), [&](char c0, char c1) { return val[c0 - 'a'] < val[c1 - 'a']; });
return s;
}
};
void testCustomSortString()
{
Solution s;
EXPECT_SUMMARY;
}
int main()
{
testCustomSortString();
return 0;
}