Challenge - 5 Problems
Anagram Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of sorting-based anagram check
What is the output of this Rust code that checks if two strings are anagrams by sorting their characters?
DSA Rust
fn are_anagrams(s1: &str, s2: &str) -> bool { let mut v1: Vec<char> = s1.chars().collect(); let mut v2: Vec<char> = s2.chars().collect(); v1.sort_unstable(); v2.sort_unstable(); v1 == v2 } fn main() { let result = are_anagrams("listen", "silent"); println!("{}", result); }
Attempts:
2 left
💡 Hint
Sorting characters and comparing vectors checks if strings have same letters.
✗ Incorrect
The function sorts both strings' characters and compares them. Since "listen" and "silent" have the same letters, the result is true.
❓ Predict Output
intermediateOutput of frequency count anagram check
What does this Rust code print when checking if two strings are anagrams using character frequency counts?
DSA Rust
use std::collections::HashMap; fn are_anagrams(s1: &str, s2: &str) -> bool { if s1.len() != s2.len() { return false; } let mut freq = HashMap::new(); for c in s1.chars() { *freq.entry(c).or_insert(0) += 1; } for c in s2.chars() { let count = freq.entry(c).or_insert(0); if *count == 0 { return false; } *count -= 1; } true } fn main() { let result = are_anagrams("triangle", "integral"); println!("{}", result); }
Attempts:
2 left
💡 Hint
Counting characters and comparing counts confirms anagrams.
✗ Incorrect
The code counts characters in first string, then decreases counts for second string. If all counts match, strings are anagrams.
❓ Predict Output
advancedOutput of anagram check ignoring case and spaces
What is the output of this Rust code that checks if two strings are anagrams ignoring case and spaces?
DSA Rust
fn normalize(s: &str) -> Vec<char> { let mut chars: Vec<char> = s.chars() .filter(|c| !c.is_whitespace()) .map(|c| c.to_ascii_lowercase()) .collect(); chars.sort_unstable(); chars } fn are_anagrams(s1: &str, s2: &str) -> bool { normalize(s1) == normalize(s2) } fn main() { let result = are_anagrams("Dormitory", "Dirty room"); println!("{}", result); }
Attempts:
2 left
💡 Hint
Removing spaces and ignoring case helps detect anagrams with different formatting.
✗ Incorrect
The code normalizes both strings by removing spaces and converting to lowercase, then sorts characters. Both normalized vectors match.
❓ Predict Output
advancedOutput of anagram check with Unicode characters
What does this Rust code print when checking if two Unicode strings are anagrams?
DSA Rust
use std::collections::HashMap; fn are_anagrams(s1: &str, s2: &str) -> bool { if s1.chars().count() != s2.chars().count() { return false; } let mut freq = HashMap::new(); for c in s1.chars() { *freq.entry(c).or_insert(0) += 1; } for c in s2.chars() { let count = freq.entry(c).or_insert(0); if *count == 0 { return false; } *count -= 1; } true } fn main() { let result = are_anagrams("résumé", "sérumé"); println!("{}", result); }
Attempts:
2 left
💡 Hint
Counting Unicode characters correctly is key for anagram checks with accents.
✗ Incorrect
The code counts Unicode characters properly and compares frequencies. Both strings have same characters with accents.
🧠 Conceptual
expertChoosing the best anagram check method for large inputs
Which anagram check technique is most efficient for very large strings with only ASCII letters?
Attempts:
2 left
💡 Hint
Consider time complexity and fixed character set size.
✗ Incorrect
Counting frequencies with a fixed-size array (size 26 for ASCII letters) is O(n) and uses constant space, faster than sorting O(n log n). Nested loops are O(n²). Sets ignore duplicates and fail for anagrams with repeated letters.
