1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
struct Solution; use std::collections::HashMap; impl Solution { fn custom_sort_string(s: String, t: String) -> String { let mut hm: HashMap<char, usize> = HashMap::new(); for (i, c) in s.char_indices() { *hm.entry(c).or_default() = i; } let mut t: Vec<char> = t.chars().collect(); t.sort_unstable_by_key(|c| hm.get(c).unwrap_or(&26)); t.iter().collect() } } #[test] fn test() { let s = "cba".to_string(); let t = "abcd".to_string(); let res = "cbad".to_string(); assert_eq!(Solution::custom_sort_string(s, t), res); }