Skip to content

Practice Custom Sort String

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#791
LeetCode Problem

Custom Sort String

Problem Statement

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.

Source: Custom Sort String on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

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

# Example 2

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

Constraints

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

Related LeetCode Problems

Practice more problems similar to Custom Sort String.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.