bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch
🌱Newbie
0 XP
Back to Valid Anagram
āœļøFill the BlankrustEasyArrays & Hashing

Valid Anagram - Fill the Blank (Rust)

Complete the Rust solution

Problem Brief

Given two strings s and t, return true if t is an anagram of s, and false otherwise. 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. Read the blank inside this exact statement: "if (t.len() ___ s.len()){".

  2. Fill in the key parts of the rust solution. Each blank represents a critical piece of the algorithm.

  3. Check the variable that is read immediately after the blank; that usually tells you what the blank must produce.

Asked at 21 companies
AffirmAmazonApple
Valid Anagram — Frequency Count
hashmap
1
5

Input: s="anagram", t="nagaram". Count char frequencies

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

Drag tokens into the blanks

!=
true
false
null
1impl Solution {
2 pub fn is_anagram(s: String, t: String) -> bool {
3 if (t.len() ___ s.len()){
4 return ___;
5 }
6 let mut map: HashMap<char, i64> = HashMap::new();
7 for (a, b) in s.chars().zip(t.chars()){
8 *map.entry(a).or_default() += 1;
9 *map.entry(b).or_default() -= 1;
10 }
11 map.into_values().all(|cnt| cnt == 0)
12 }
13}