From 7c60dee83b436e8e8a667bf9f191beced8de4fc3 Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Mon, 10 Mar 2025 00:36:13 -0600 Subject: chore: move cpp and rust code to directory --- other/group_anagrams.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 other/group_anagrams.rs (limited to 'other/group_anagrams.rs') diff --git a/other/group_anagrams.rs b/other/group_anagrams.rs new file mode 100644 index 0000000..212fc15 --- /dev/null +++ b/other/group_anagrams.rs @@ -0,0 +1,15 @@ +use std::collections::HashMap; +struct Solution; + +impl Solution { + pub fn group_anagrams(strs: Vec) -> Vec> { + let mut groups = HashMap::new(); + for s in strs { + let hash = s.bytes().fold([0;26], |mut h, b| { + h[(b - b'a') as usize] += 1; h + }); + groups.entry(hash).or_insert_with(Vec::new).push(s); + } + groups.into_values().collect() + } +} -- cgit v1.2.3