Custom Sort String
Real-world version: Rearranging a Label to a Given Letter OrderThe statement here is our own: the same inputs, outputs and technique.
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.
Examples
Example 1
- Input
- order = "fedcba", s = "abcdefghij"
- Output
- "fedcbaghij"
The order reverses the first six letters, so those come out backwards, and the four letters not named there follow alphabetically.
Example 2
- Input
- order = "ba", s = "aabbcc"
- Output
- "bbaacc"
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
- Input
- order = "abc", s = "zyx"
- Output
- "xyz"
None of the label's letters are named in the order, so they simply come out alphabetically.
Constraints
- 1 <= order.length <= 26
- 1 <= s.length <= 200
- order and s consist of lowercase English letters only
- The letters of order are all different
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def custom_sort_string(order: str, s: str) -> str:public String customSortString(String order, String s)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Valid AnagramReal-world version: Matching Letter TallyEasy
- Find the DifferenceReal-world version: The Sort That Joined the TrayEasy
- Sort the PeopleReal-world version: Season Leaderboard From Two Parallel ListsEasy
- Find Resultant Array After Removing AnagramsReal-world version: Clearing Rearranged Labels Off the ShelfEasy