aboutsummaryrefslogtreecommitdiff
path: root/other/sort_char_freq.rs
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
committerJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
commit7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch)
treeeb7a24ee2067f92245c57bb118e85a1baf15df50 /other/sort_char_freq.rs
parent5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff)
downloadleetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz
leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip
chore: move cpp and rust code to directory
Diffstat (limited to 'other/sort_char_freq.rs')
-rw-r--r--other/sort_char_freq.rs16
1 files changed, 16 insertions, 0 deletions
diff --git a/other/sort_char_freq.rs b/other/sort_char_freq.rs
new file mode 100644
index 0000000..a9d0e13
--- /dev/null
+++ b/other/sort_char_freq.rs
@@ -0,0 +1,16 @@
+struct Solution;
+
+impl Solution {
+ pub fn frequency_sort(s: String) -> String {
+ let mut map = std::collections::HashMap::with_capacity(26 * 2);
+ s.chars().for_each(|b| * map.entry(b).or_insert(0) += 1);
+ let mut v = map.into_iter().map(|(c, n)| (c, n)).collect::<Vec<_>>();
+ v.sort_unstable_by_key(|pair| - pair.1);
+ v.into_iter().flat_map(|(c, n)| vec![c; n as usize]).collect()
+ }
+}
+
+fn main() {
+ let s = String::from("tree");
+ print!("{}", Solution::frequency_sort(s));
+}