Custom Sort String

You are given two strings order and s. All the characters of order are unique and were sorted in some custom order previously.

Permute the characters of s so that they match the order that order was sorted. More specifically, if a character x occurs before a character y in order, then x should occur before y in the permuted string.

Return any permutation of s that satisfies this property.

Example 1
Inputorder = "cba", s = "abcd"
Output"cbad"
"a", "b", and "c" must appear in the order "c", "b", "a"; "d" does not appear in order, so it can be at any position.
Example 2
Inputorder = "bcafg", s = "abcd"
Output"bcad"
The characters "b", "c", and "a" from order dictate the order for those characters in s, while "d" can be placed at any position.

Constraints

  • 1 <= order.length <= 26
  • 1 <= s.length <= 200
  • order and s consist of lowercase English letters.
  • All the characters of order are unique.

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate