-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
pub fn custom_sort_string(order: String, mut s: String) -> String { | ||
let order = order.char_indices().fold([0; 26], |mut acc, (idx, c)| { | ||
acc[c as usize - 'a' as usize] = idx; | ||
acc | ||
}); | ||
unsafe { | ||
s.as_mut_vec() | ||
.sort_by_key(|c| order[*c as usize - 'a' as usize]) | ||
}; | ||
s | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::custom_sort_string; | ||
|
||
#[test] | ||
fn sorts_in_custom_order() { | ||
let order = "cba".to_string(); | ||
let s = "abc".to_string(); | ||
let result = custom_sort_string(order, s); | ||
assert_eq!("cba".to_string(), result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,3 +50,4 @@ mod max_repeating_substr; | |
mod freq_sort; | ||
mod sort_jumbled_numbers; | ||
mod max_binary_number; | ||
mod custom_sort; |