-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path791.cpp
More file actions
31 lines (31 loc) · 829 Bytes
/
791.cpp
File metadata and controls
31 lines (31 loc) · 829 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
25
26
27
28
29
30
31
struct Compare {
vector<int> mp;
Compare(vector<int>& mapping) {
mp = mapping;
}
bool operator() (const char c1, const char c2) {
return mp[c1 - 'a'] < mp[c2 - 'a'];
}
};
class Solution {
public:
string customSortString(string order, string s) {
vector<int> mapping(26, -1);
for (int i = 0; i < order.size(); ++i) {
mapping[order[i] - 'a'] = i;
}
vector<char> temp;
for (auto& c : s) {
if (mapping[c - 'a'] != -1) temp.push_back(c);
}
sort(temp.begin(), temp.end(), Compare(mapping));
int index = 0;
for (int i = 0; i < s.size(); ++i) {
if (mapping[s[i] - 'a'] != -1) {
s[i] = temp[index];
index++;
}
}
return s;
}
};