Good evening! Here's our prompt for today.
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.

For example, consider the following strings as input:
1const order = 'fed';
2const 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
ands
consist of lowercase English letters.- All the characters of
order
are unique.
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx
var assert = require('assert');
​
function custom_sort_string(order, s) {
//fill in the solution
return;
}
​
try {
assert.equal(custom_sort_string('fed', 'adef'), 'feda');
​
console.log('PASSED: ' + "custom_sort_string('fed', 'adef') should return `feda`");
} catch (err) {
console.log(err);
}
​
try {
assert.equal(custom_sort_string('cba', 'abcd'), 'cbad');
​
console.log('PASSED: ' + "custom_sort_string('cba', 'abcd') should return `cbad`");
} catch (err) {
console.log(err);
}
​
try {
assert.equal(custom_sort_string('cbafg', 'abcd'), 'cbad');
​
console.log('PASSED: ' + "custom_sort_string('cbafg', 'abcd') should return `cbad`");
} catch (err) {
console.log(err);
Here's our guided, illustrated walk-through.
How do I use this guide?
Access all course materials today
The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.