791. Custom Sort String
https://leetcode.com/problems/custom-sort-string/
/*
* @lc app=leetcode id=791 lang=cpp
*
* [791] Custom Sort String
*/
// @lc code=start
class Solution {
public:
string customSortString(string S, string T) {
vector<int> freqs(26, 0);
for (const auto t : T) ++freqs[t - 'a'];
string res(T.size(), ' ');
int c = 0;
for (const auto s : S) {
for (int i = 0; i < freqs[s - 'a']; ++i) {
res[c++] = s;
}
freqs[s - 'a'] = 0;
}
for (int i = 0; i < 26; ++i) {
for (int j = 0; j < freqs[i]; ++j) {
res[c++] = 'a' + i;
}
}
return res;
}
};
// @lc code=end
Last updated