//
//  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;
}
