Trains the technique from
LeetCode 791Custom Sort StringThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A letter order is given as order, a string of distinct lowercase letters, and a label as s.
Rearrange the letters of s so that any two letters both appearing in order come in the same relative order as they do there. Letters of s not appearing in order all come after those that do, arranged alphabetically among themselves.
Return the rearranged label. Every letter of s is kept, repeats included.
Example 1
The order reverses the first six letters, so those come out backwards, and the four letters not named there follow alphabetically.
Example 2
The order asks for b before a, so both b's come first, then both a's, and the unnamed c's follow.
Example 3
None of the label's letters are named in the order, so they simply come out alphabetically.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def custom_sort_string(order: str, s: str) -> str:public String customSortString(String order, String s)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.