Skip to content

Instantly share code, notes, and snippets.

@wunki
Created June 2, 2016 09:14
Show Gist options
  • Save wunki/e8a3f1d8a6dbc7cf3f8f8ffe42f5c7f9 to your computer and use it in GitHub Desktop.
Save wunki/e8a3f1d8a6dbc7cf3f8f8ffe42f5c7f9 to your computer and use it in GitHub Desktop.
use std::collections::HashMap;
type CharCount = HashMap<char, u16>;
fn word_to_hashmap(word: &str) -> CharCount {
let mut charcount = HashMap::new();
for c in word.chars() {
let c = c.to_lowercase().next().unwrap();
let count = charcount.entry(c).or_insert(0);
*count += 1;
}
charcount
}
pub fn anagrams_for<'a>(word: &str, inputs: &'a [&str]) -> Vec<&'a str> {
let mut outputs: Vec<&'a str> = vec![];
let wordhash = word_to_hashmap(word);
for i in inputs.iter() {
if i.to_lowercase() == word.to_lowercase() { continue }
if wordhash == word_to_hashmap(i) {
outputs.push(i);
}
}
outputs
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment