From dcbe6a126ce94709cf0fec18170b49e8d6e5ab87 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 11 Mar 2024 23:09:50 +0100 Subject: [PATCH] =?UTF-8?q?rs:=20add=20=C2=AB791.=20Custom=20Sort=20String?= =?UTF-8?q?=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- rs/custom-sort-string.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 rs/custom-sort-string.rs 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() + } +}