235. Custom Sort String

MediumStringString

Given a permutation 'order' of some letters and a string s, sort the characters of s so that they follow the relative ordering in 'order'. Characters not present in 'order' may appear in any position (place them after ordered ones). Input: '"order", "s"'.

Input: '"order", "s"'.

Output: Quoted string — the custom-sorted string.

Examples

Example 1
Input: "cba", "abcd"
Output: "cbad"
Explanation: c,b,a follow order; d appended.
Example 2
Input: "bcafg", "abcd"
Output: "bcad"
Explanation: Order applied, d after.
Example 3
Input: "abc", "abc"
Output: "abc"
Explanation: Already ordered.

Constraints

Asked by

MetaAmazonBloombergGoogleMicrosoft
Solve this problem in the editor →