Skip to content

Instantly share code, notes, and snippets.

@wunki wunki/lib.rs
Created Jun 2, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.