#791
Medium Algorithms Custom sort string
Hash Table String Sorting
72.3% acceptance
Feb 21, 2026
3822
431
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.
Solution
Rust
Time O(n log n)
Space O(1)
/*
* 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.
* Example 1:
* Input: order = "cba", s = "abcd"
* Output: "cbad"
* Explanation: "a", "b", "c" appear in order, so the order of "a", "b", "c" should be "c", "b", and "a".
* Since "d" does not appear in order, it can be at any position in the returned string. "dcba", "cdba", "cbda" are also valid outputs.
* Example 2:
* Input: order = "bcafg", s = "abcd"
* Output: "bcad"
* Explanation: The characters "b", "c", and "a" from order dictate the order for the characters in s. The character "d" in s does not appear in order, so its position is flexible.
* Following the order of appearance in order, "b", "c", and "a" from s should be arranged as "b", "c", "a". "d" can be placed at any position since it's not in order. The output "bcad" correctly follows this rule. Other arrangements like "dbca" or "bcda" would also be valid, as long as "b", "c", "a" maintain their order.
* Constraints:
* 1 <= order.length <= 26
* 1 <= s.length <= 200
* order and s consist of lowercase English letters.
* All the characters of order are unique.
*/
impl Solution {
pub fn custom_sort_string(order: String, s: String) -> String {
let mut rank = [26usize; 26];
for (i, c) in order.chars().enumerate() {
rank[c as usize - 'a' as usize] = i;
}
let mut chars: Vec<char> = s.chars().collect();
chars.sort_by_key(|&c| rank[c as usize - 'a' as usize]);
chars.into_iter().collect()
}
}