diff --git a/rs/custom-sort-string.rs b/rs/custom-sort-string.rs new file mode 100644 index 0000000..b7fd81b --- /dev/null +++ b/rs/custom-sort-string.rs @@ -0,0 +1,13 @@ +use std::collections::HashMap; + +impl Solution { + pub fn custom_sort_string(order: String, s: String) -> String { + let weights: HashMap = + order.chars().enumerate().map(|(i, c)| (c, i)).collect(); + + let mut s: Vec = s.chars().collect(); + s.sort_unstable_by_key(|c| *weights.get(c).unwrap_or(&usize::MAX)); + + s.into_iter().collect() + } +}