bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch
🌱Newbie
0 XP
Back to Group Anagrams
🧩Arrange CoderustMediumArrays & Hashing

Group Anagrams - Arrange Code (Rust)

Put the Rust code in order

Problem Brief

Given an array of strings strs, group the anagrams together. You can return the answer in any order. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Puzzle Hints
  1. Ignore visual position first; place the line that creates required state before lines that read it. One candidate is "pub fn group_anagrams(strs: Vec<String>) -> Vec<Vec<String>> {".

  2. Arrange the rust code lines in the correct order to form a working solution.

  3. When two lines both look plausible, choose the one whose output is needed by the next line.

Asked at 36 companies
AdobeAffirmAmazon
Group Anagrams — Sorted Key HashMap
hashmap
1
8

Input: ["eat","tea","tan","ate","nat","bat"]. Key = sorted chars

HashMap & HashSet in Rustref

Rust std::collections provides HashMap<K,V> and HashSet<T>. Use entry() API for elegant insert-or-update. The borrow checker ensures safe concurrent access. or_insert(default) and or_insert_with(|| ...) handle missing keys.

-HashMap::new() — O(1) average insert/get/remove
-entry(key).or_insert(default) — insert if missing
-get(&key) returns Option<&V>
-HashSet — insert/contains/remove O(1)
use std::collections::{HashMap, HashSet};

let mut map = HashMap::new();
map.insert("a", 1);
*map.entry("b").or_insert(0) += 1;

let mut set = HashSet::new();
set.insert(42);
set.contains(&42);  // true
Official docs →
HashMap & HashSet in Rustref

Rust std::collections provides HashMap<K,V> and HashSet<T>. Use entry() API for elegant insert-or-update. The borrow checker ensures safe concurrent access. or_insert(default) and or_insert_with(|| ...) handle missing keys.

-HashMap::new() — O(1) average insert/get/remove
-entry(key).or_insert(default) — insert if missing
-get(&key) returns Option<&V>
-HashSet — insert/contains/remove O(1)
use std::collections::{HashMap, HashSet};

let mut map = HashMap::new();
map.insert("a", 1);
*map.entry("b").or_insert(0) += 1;

let mut set = HashSet::new();
set.insert(42);
set.contains(&42);  // true
Official docs →
How to think: Hash Map / Setguide

You need O(1) lookups — checking if something exists, counting frequencies, or finding pairs.

1.Ask: "Am I searching for something repeatedly?" → Hash Map
2.Ask: "Do I need to check existence?" → Set
3.Ask: "Do I need to count occurrences?" → Map with value = count
4.Ask: "Do I need to find a pair that satisfies a condition?" → Store complement in Map
5.The tradeoff: O(n) extra space buys you O(1) per lookup

vs Nested loops (O(n²)): You're comparing every element against every other — a Map does it in one pass

vs Sorting (O(n log n)): You just need existence/frequency checks, not order

find pairtwo numbers thatfrequencycountduplicateanagramgroup by
How to think: Hash Map / Setguide

You need O(1) lookups — checking if something exists, counting frequencies, or finding pairs.

1.Ask: "Am I searching for something repeatedly?" → Hash Map
2.Ask: "Do I need to check existence?" → Set
3.Ask: "Do I need to count occurrences?" → Map with value = count
4.Ask: "Do I need to find a pair that satisfies a condition?" → Store complement in Map
5.The tradeoff: O(n) extra space buys you O(1) per lookup

vs Nested loops (O(n²)): You're comparing every element against every other — a Map does it in one pass

vs Sorting (O(n log n)): You just need existence/frequency checks, not order

find pairtwo numbers thatfrequencycountduplicateanagramgroup by
  • 1 pub fn group_anagrams(strs: Vec<String>) -> Vec<Vec<String>> {
  • 2 let mut map: std::collections::HashMap<String, Vec<String>> = std::collections::HashMap::new();
  • 3}
  • 4impl Solution {
  • 5 let key = String::from_utf8(key).unwrap();
  • 6 }
  • 7 map.into_values().collect()
  • 8 map.entry(key).or_default().push(s);
  • 9 key.sort();
  • 10 }
  • 11 for s in strs {
  • 12 let mut key: Vec<u8> = s.bytes().collect();