-
Notifications
You must be signed in to change notification settings - Fork 1
/
customSortString.cpp
44 lines (40 loc) · 975 Bytes
/
customSortString.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
44
#include <string>
#include <unordered_map>
using namespace std;
class Solution {
public:
string customSortString(string S, string T) {
string ans = T;
unordered_map<char,int> lets;
for(auto it : T){
if(lets.find(it)==lets.end())lets.insert({it,1});
else lets[it]++;
}
int j = T.size()-1;
for (int i = S.size()-1;i>=0;i--){
if(lets.find(S[i])==lets.end()){
}
else{
while(lets[S[i]]>0){
ans[j]=S[i];
lets[S[i]]--;
j--;
}
}
}
for(auto it : T){
while(lets[it]>0)
{
ans[j]=it;
lets[it]--;
j--;
}
}
if(T.size()==1)return T;
return ans;
}};
int main(){
Solution s;
s.customSortString("cba","abcd");
return 0;
}