Community

Start a Thread


Notifications
Subscribe You’re not receiving notifications from this thread.

Custom Sort String (Main Thread)

Here is the interview question prompt, presented for reference.

We are given two strings order and str, with the string order already sorted in a custom order. Permute the characters of str such that they match the custom order in which order is sorted. More specifically, if a character x occurs before a character y in order, then x should occur before y in the permuted string.

![image](https://storage.googleapis.com/algodailyrandomassets/curriculum/easy-strings/custom-sort-string/problem.png)

For example, consider the following strings as input:

String order = "fed";
String str = "adef";
const order = 'fed';
const str = 'adef';
order = 'fed'
str = 'adef'
std::string order = "fed";
std::string str = "adef";
string order = "fed";
string str = "adef";
order := "fed"
str := "adef"

The output for the given string will be "feda". "d", "e", "f" in str are arranged according to the order string, which makes the order of "d", "e", "f" as "f", "e", "d". Since "a" does not appear in the defined order, it should appear at the end of the string.

Constraints

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

You can see the full challenge with visuals at this link.

Challenges • Asked over 2 years ago by Jake from AlgoDaily

Jake from AlgoDaily Commented on Aug 14, 2022:

This is the main discussion thread generated for Custom Sort String (Main Thread).