Dsa
Strings
Medium
Custom String Sort
You are given two strings: 'order' and 'str'. The 'order' string contains a collection of unique characters which determines the priority of the characters in 'str'. Your task is to sort 'str' such that the characters appear in the order they are found in 'order'. Characters not in 'order' should be appended at the end in their original relative order. If 'order' is empty, return 'str' as is.
Example Input:
'cba'
'abdc'
Example Output:
'dcbab'
Key concepts
stringssortingcustom_order
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now