//
// main.cpp
// Custom Sort
//
// Created by Himanshu on 16/08/21.
//
#include <iostream>
#include <string>
using namespace std;
//String S is order & string T is the given string
string customSortString(string S, string T) {
string ans = "";
for (int i=0; i<S.size(); i++) {
for (int j=0; j<T.size(); j++) {
if (T[j] == S[i]) {
ans.push_back(S[i]);
//marking j character as selected
T[j] = 'A';
}
}
}
for (int i=0; i<T.size(); i++) {
if (T[i] != 'A') {
//Adding remaining (not selected before) characters to the answer
ans.push_back(T[i]);
}
}
return ans;
}
int main() {
// your code goes here
cout<<customSortString("cba", "abcd")<<endl;
cout<<customSortString("cbafg", "abcd")<<endl;
cout<<customSortString("cba", "dbca")<<endl;
cout<<customSortString("cbahkj", "hkabc")<<endl;
return 0;
}
Ly8KLy8gIG1haW4uY3BwCi8vICBDdXN0b20gU29ydAovLwovLyAgQ3JlYXRlZCBieSBIaW1hbnNodSBvbiAxNi8wOC8yMS4KLy8KCiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPHN0cmluZz4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCi8vU3RyaW5nIFMgaXMgb3JkZXIgJiBzdHJpbmcgVCBpcyB0aGUgZ2l2ZW4gc3RyaW5nCnN0cmluZyBjdXN0b21Tb3J0U3RyaW5nKHN0cmluZyBTLCBzdHJpbmcgVCkgewogICAgc3RyaW5nIGFucyA9ICIiOwogICAgZm9yIChpbnQgaT0wOyBpPFMuc2l6ZSgpOyBpKyspIHsKICAgICAgICBmb3IgKGludCBqPTA7IGo8VC5zaXplKCk7IGorKykgewogICAgICAgICAgICBpZiAoVFtqXSA9PSBTW2ldKSB7CiAgICAgICAgICAgICAgICBhbnMucHVzaF9iYWNrKFNbaV0pOwogICAgICAgICAgICAgICAgLy9tYXJraW5nIGogY2hhcmFjdGVyIGFzIHNlbGVjdGVkCiAgICAgICAgICAgICAgICBUW2pdID0gJ0EnOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgZm9yIChpbnQgaT0wOyBpP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