Initial commit

This commit is contained in:
2022-08-04 23:58:25 +02:00
commit ff1c36fc24
447 changed files with 9869 additions and 0 deletions

25
anagram/src/lib.rs Normal file
View File

@@ -0,0 +1,25 @@
use std::collections::HashSet;
pub fn anagrams_for<'a>(word: &str, possible_anagrams: &[&'a str]) -> HashSet<&'a str> {
let to_lowercase_s = |c: char| {
if !c.is_lowercase() {
c.to_lowercase().to_string()
} else {
c.to_string()
}
};
let lw: Vec<_> = word.chars().map(to_lowercase_s).collect();
let mut sc: Vec<_> = word.chars().map(to_lowercase_s).collect();
sc.sort_unstable();
let result = possible_anagrams.iter()
.filter(|w| {
let mut sci: Vec<_> = w.chars().map(to_lowercase_s).collect();
let same_word = lw == sci;
sci.sort_unstable();
sci == sc && !same_word
}).map(|&w| w);
HashSet::from_iter(result)
}