Skip to main content

Custom Sort String

MediumPremium

You're given two strings, order and s. Every character in order is unique, and order defines a custom sort order. Rearrange the characters of s so they follow that order: if x comes before y in order, every x must come before every y in the result.

Characters of s that don't appear in order go at the end, in the same order they appear in s.

Example 1

Input: order = "cba", s = "abcd" Output: "cbad"

c, b and a follow the custom order. d isn't in order, so it goes at the end.

Example 2

Input: order = "bcafg", s = "abcd" Output: "bcad"

f and g are in order but not in s, so they don't appear in the result.

Constraints

  • 1 <= order.length <= 26
  • 1 <= s.length <= 200
  • order and s contain only lowercase English letters, and every character of order is unique.