解题思路:
一道很简单但是很有意义的题目,是对map和自定义排序的灵活应用,首先用map、记录order中字符的相对位置(字符是唯一的),接着相对于map中存储的位置进行自定义排序,作用在s中即可,代码如下:
unordered_map<char, int> mp;
bool cmp(const char a, const char b) {
return mp[a] < mp[b];
}
class Solution {
public:
string customSortString(string order, string s) {
for(int i = 0; i < order.size(); i ++) {
mp[order[i]] = i;
}
sort(s.begin(), s.end(), cmp);
return s;
}
};