rs: add «791. Custom Sort String»
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
dbd0883d68
commit
dcbe6a126c
1 changed files with 13 additions and 0 deletions
13
rs/custom-sort-string.rs
Normal file
13
rs/custom-sort-string.rs
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
impl Solution {
|
||||||
|
pub fn custom_sort_string(order: String, s: String) -> String {
|
||||||
|
let weights: HashMap<char, usize> =
|
||||||
|
order.chars().enumerate().map(|(i, c)| (c, i)).collect();
|
||||||
|
|
||||||
|
let mut s: Vec<char> = s.chars().collect();
|
||||||
|
s.sort_unstable_by_key(|c| *weights.get(c).unwrap_or(&usize::MAX));
|
||||||
|
|
||||||
|
s.into_iter().collect()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue