use std::collections::HashMap;
#[derive(Clone)]
struct TrieNode {
children: HashMap<char, TrieNode>,
is_end_of_word: bool,
}
fn prefix_match_trie(root: &TrieNode, prefix: &str) -> Vec<String> {
let mut current = root;
for ch in prefix.chars() {
match current.children.get(&ch) {
Some(child) => current = child,
None => return vec![],
}
}
// Collect all words from prefix node
let mut results = vec![];
collect_words(current, prefix.to_string(), &mut results);
results
}
fn collect_words(node: &TrieNode, mut current_word: String, results: &mut Vec<String>) {
if node.is_end_of_word {
results.push(current_word.clone());
}
for (ch, child) in node.children.iter() {
current_word.push(*ch);
collect_words(child, current_word.clone(), results);
current_word.pop();
}
}
fn prefix_match_hashmap(map: &HashMap<String, String>, prefix: &str) -> Vec<String> {
map.keys()
.filter(|k| k.starts_with(prefix))
.cloned()
.collect()
}Complete Rust implementations: Trie uses node traversal for prefix and DFS to collect descendant words. HashMap filters keys starting with the prefix.