Sort a String with the Given Template

Published by Mubashir Hassan in

The function is given two strings t - template, s - to be sorted. Sort the characters in s such that if the character is present in t then it is sorted according to the order in t and other characters are sorted alphabetically after the ones found in t.

Examples

customSort("edc", "abcdefzyx") ➞ "edcabfxyz"

customSort("fby", "abcdefzyx") ➞ "fbyacdexz"

customSort("", "abcdefzyx") ➞ "abcdefxyz"

customSort("", "") ➞ ""

Notes

The characters in t and s are all lower-case.

Watch a quick demo on how Edabit works.