Custom Sort String
You are provided with the two strings named X and Y respectively. Y has its own specific order and has no repeating characters. Your task is to arrange the characters of the first string i.e. X in such a way that the order of characters in X is exactly the same as in Y, which means if ‘d’ occurs after ‘c’ in Y then it should also occur after ‘c’ in X ( obviously if X has ‘d’ and ‘c’ as characters in it ). All you have to do is, convert string X in the specific order with respect to string Y.
Both the strings have only lowercase English alphabets. There may be more than one correct solution, you have to return any one of the possible solutions.
The first line of input contains an integer ‘T’ denoting the number of test cases. Then the T test cases follow. The first line of each test case contains the string X. The second line of each test case contains the string Y.
For each test case, print the string X after converting string X in the specific order with respect to string Y. The output of each test case is printed in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10 1 <= |X| <= 10000 1 <= |Y| <= 26 where ‘T’ is the number of test cases, |X| is the length of the first string and |Y| is the length of the second string. Time Limit: 1 sec
The idea is to follow the order in which the characters occur in string Y. We have to run two nested loops on strings Y and X respectively and if Yi is the same as Xj then pick this character and add it to the answer string.
For the remaining characters of X that are not in the string Y, we have to just add those characters in our answer string. To find those remaining characters we can create a vector of visited characters or a hash set.