Skip to content
DeepForgeby svx
All problems
MediumAlgorithmsal-143

Custom Sort String

Problem

Rearrange the characters of s so they appear in the order given by order, followed by any remaining characters in alphabetical order.

Count the characters of s, emit the counts in the order string, then emit the leftover characters sorted. Characters of order that do not occur in s are skipped.

Hint

Delete characters from the count table as you emit them so leftovers are easy to append.

Test cases

case 1
in: ["cba","abcd"]
expected: "cbad"
case 2
in: ["bcafg","abcd"]
expected: "bcad"
case 3
in: ["exv","xwvee"]
expected: "eexvw"
case 4
in: ["","abc"]
expected: "abc"
case 5
in: ["abc",""]
expected: ""
Starter codePython
def custom_sort_string(order, s):
    # Your code here
    pass

5 test cases · run your code against them in the editor