Complete the code to create a new empty HashMap for grouping anagrams.
let mut map: std::collections::HashMap<String, Vec<String>> = [1];We use HashMap::new() to create an empty hash map for grouping anagrams.
Complete the code to sort the characters of the word to use as a key.
let mut chars: Vec<char> = word.chars().collect();
chars.[1]();
let key: String = chars.into_iter().collect();We use sort_unstable() to sort characters efficiently for the anagram key.
Fix the error in inserting the word into the map for the sorted key.
map.entry(key).[1](Vec::new()).push(word.clone());or_insert() inserts a new vector if the key is missing and returns a mutable reference to it.
Fill both blanks to collect grouped anagrams into a vector of vectors.
let result: Vec<Vec<String>> = map.[1]().[2](|(_, group)| group.clone()).collect();
We use into_iter() to consume the map and map() to extract the groups.
Fill all three blanks to complete the function that groups anagrams.
fn group_anagrams(strs: Vec<String>) -> Vec<Vec<String>> {
let mut map = std::collections::HashMap::new();
for word in strs {
let mut chars: Vec<char> = word.chars().[1]();
chars.[2]();
let key: String = chars.into_iter().collect();
map.entry(key).[3](Vec::new()).push(word);
}
map.into_iter().map(|(_, group)| group).collect()
}We use collect() to gather characters into a vector, sort_unstable() to sort them for the anagram key, and or_insert() to insert or get the group vector.
