-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
43 lines (35 loc) · 940 Bytes
/
main.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
#include <bits/stdc++.h>
#include <gtest/gtest.h>
using namespace std;
class Solution {
public:
string customSortString(string order, string s) {
unordered_map<char, int> fqs;
for (const auto c : s) fqs[c]++;
string ans;
ans.reserve(order.length());
for (const auto c : order)
if (fqs.contains(c)) ans.append(fqs[c], c);
for (const auto c : s)
if (find(order.begin(), order.end(), c) == order.end()) ans += c;
return ans;
}
};
TEST(SolutionTest, Test1) {
Solution solution;
string order{"cba"};
string s{"abcd"};
EXPECT_EQ("cbad", solution.customSortString(order, s));
}
TEST(SolutionTest, Test2) {
Solution solution;
string order{"bcafg"};
string s{"abcd"};
EXPECT_EQ("bcad", solution.customSortString(order, s));
}
TEST(SolutionTest, Test3) {
Solution solution;
string order{"exv"};
string s{"xwvee"};
EXPECT_EQ("eexvw", solution.customSortString(order, s));
}