From c689e2ad8273b22e1ce8ae4ec58013d3e43010dc Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Fri, 5 Nov 2021 22:58:26 +0000 Subject: [rust] Anagram --- rust/anagram/src/lib.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 rust/anagram/src/lib.rs (limited to 'rust/anagram/src/lib.rs') diff --git a/rust/anagram/src/lib.rs b/rust/anagram/src/lib.rs new file mode 100644 index 0000000..4602db0 --- /dev/null +++ b/rust/anagram/src/lib.rs @@ -0,0 +1,30 @@ +use std::collections::HashSet; + +pub fn anagrams_for<'a>(word: &str, possible_anagrams: &[&'a str]) -> HashSet<&'a str> { + let word = word.to_lowercase(); + let word_sign = signature(&word); + possible_anagrams.iter().filter(|a| { + let a_lower = a.to_lowercase(); + word_sign == signature(&a_lower) && word != a_lower + }).cloned().collect() +} + +/// Compute the signature of a word. +/// +/// In this case the signature of a word is the ordered list of its lowercased +/// UTF8 characters. +fn signature(word: &str) -> Vec<&str> { + let mut chars: Vec<&str> = vec![]; + if !word.is_empty() { + let mut start = 0; + for i in (start+1)..word.len() { + if word.is_char_boundary(i) { + chars.push(word.get(start..i).unwrap()); + start = i; + } + } + chars.push(word.get(start..).unwrap()); + chars.sort_unstable(); + } + chars +} -- cgit v1.2.3