naive implementation of anagram
This commit is contained in:
parent
988ca31e2b
commit
bbe4e7fbc3
2 changed files with 24 additions and 8 deletions
|
@ -1,9 +1,27 @@
|
|||
use std::collections::HashSet;
|
||||
|
||||
pub fn anagrams_for<'a>(word: &str, possible_anagrams: &[&str]) -> HashSet<&'a str> {
|
||||
unimplemented!(
|
||||
"For the '{}' word find anagrams among the following words: {:?}",
|
||||
word,
|
||||
possible_anagrams
|
||||
);
|
||||
pub fn anagrams_for<'a>(word: &str, possible_anagrams: &[&'a str]) -> HashSet<&'a str> {
|
||||
let mut word_set = HashSet::new();
|
||||
for char in word.chars(){
|
||||
word_set.insert(char.clone().to_ascii_uppercase());
|
||||
word_set.insert(char.clone().to_ascii_lowercase());
|
||||
}
|
||||
let mut output:HashSet<&'a str> = HashSet::new();
|
||||
for possible_anagram in possible_anagrams.iter(){
|
||||
if *possible_anagram == word{ continue; }
|
||||
let mut anagram_set = word_set.clone();
|
||||
for char in possible_anagram.chars(){
|
||||
if anagram_set.remove(&char){
|
||||
anagram_set.remove(&char.to_ascii_uppercase());
|
||||
anagram_set.remove(&char.to_ascii_lowercase());
|
||||
}
|
||||
else{
|
||||
break;
|
||||
}
|
||||
}
|
||||
if anagram_set.len() == 0{
|
||||
output.insert(&possible_anagram);
|
||||
}
|
||||
}
|
||||
output
|
||||
}
|
||||
|
|
|
@ -20,7 +20,6 @@ fn test_no_matches() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_detect_simple_anagram() {
|
||||
let word = "ant";
|
||||
|
||||
|
@ -32,7 +31,6 @@ fn test_detect_simple_anagram() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_does_not_confuse_different_duplicates() {
|
||||
let word = "galea";
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue