InterviewBit Academy is now Scaler!
InterviewBit Academy is now Scaler Academy!

Custom Sort String

Given two string A and B, both consisting of lowercase letters. A consists of uniques lowercase English letter
i.e. no characters occur twice in it.

A was sorted in some custom order previously. We want to permute the characters of B so that they match the order that A was sorted.

More specifically, if character x occurs before y in A,
then x should occur before y in the returned string.
For the elements not present in A, append them at last in lexicographical order.

Return the permutation of string B that matches the above criteria.

Input Format

The First argument given is String A.
The Second argument given is String B.

Output Format

Return the permutation of string B that matches the given criteria.


1 <= length of the string B <= 100000
1 <= length of the string A <= 26

For Example

Input 1:
    A = "cda"
    B = "abcd"
    Explanation 1:
        character 'c' should occur before 'a' and 'd'.
        character 'd' should occur before 'a' 
        character 'b' is not present in A therefore it is appended at the end.

Input 2:
    A = "erhdqo"
    B = "wwdybr"
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Custom Sort String on Interview Code Editor
Sign Up
to access hints and editorial solutions for Custom Sort String


Click here to start solving coding interview questions