📖 Scenario: You are helping a librarian organize books by checking if two book titles are anagrams. Anagrams are words or phrases made by rearranging the letters of another, ignoring spaces and case.
🎯 Goal: Build a Rust program that checks if two given strings are anagrams using a simple technique.
📋 What You'll Learn
Create two string variables with exact values
Add a helper variable to prepare strings for comparison
Write core logic to check if the two strings are anagrams
Print the result as true or false
💡 Why This Matters
🌍 Real World
Checking anagrams helps in organizing and searching text data, such as book titles or names, by grouping similar words.
💼 Career
Understanding string manipulation and comparison is essential for software development roles involving text processing, data cleaning, and search algorithms.
Progress0 / 4 steps
1
Create two string variables
Create two string variables called first and second with these exact values: "Listen" and "Silent"
DSA Rust
Hint
Use String::from to create string variables.
2
Prepare strings for comparison
Add two new variables called first_chars and second_chars that store the lowercase sorted characters of first and second respectively, ignoring spaces
DSA Rust
Hint
Use to_lowercase(), chars(), filter(), and sort_unstable() to prepare the strings.
3
Check if strings are anagrams
Create a boolean variable called are_anagrams that is true if first_chars equals second_chars, otherwise false
DSA Rust
Hint
Use the equality operator == to compare the sorted character vectors.
4
Print the result
Print the value of are_anagrams using println!
DSA Rust
Hint
Use println!("{}", are_anagrams); to print the boolean result.
Practice
(1/5)
1. What is the main idea behind checking if two strings are anagrams?
easy
A. Both strings have the same characters with the same frequency
B. Both strings have the same length only
C. Both strings start with the same character
D. Both strings contain only alphabets
Solution
Step 1: Understand anagram definition
Anagrams are words or phrases made by rearranging letters of another, so character frequency must match.
Step 2: Compare character frequencies
Checking if both strings have the same characters with the same counts confirms they are anagrams.
Final Answer:
Both strings have the same characters with the same frequency -> Option A
Quick Check:
Anagram check = same characters & frequency [OK]
Hint: Check if character counts match in both strings [OK]
Common Mistakes:
Only comparing string lengths
Ignoring character frequency
Checking only first characters
2. Which Rust code snippet correctly sorts the characters of a string s to help check anagrams?
easy
A. let mut chars: Vec<char> = s.chars().collect(); chars.sort();
B. let chars = s.sort();
C. let chars = s.chars().sorted();
D. let chars = s.to_vec().sort();
Solution
Step 1: Collect characters into a vector
Use s.chars().collect() to get a vector of characters from the string.
Step 2: Sort the vector in place
Call chars.sort() to sort characters alphabetically for comparison.
Final Answer:
let mut chars: Vec<char> = s.chars().collect(); chars.sort(); -> Option A
Quick Check:
Collect chars then sort vector [OK]
Hint: Collect chars into Vec then call sort() [OK]
Common Mistakes:
Trying to sort string directly
Using non-existent sorted() method
Calling sort() on &str instead of Vec<char>
3. What is the output of this Rust code snippet?
fn main() {
let s1 = "listen";
let s2 = "silent";
let mut v1: Vec<char> = s1.chars().collect();
let mut v2: Vec<char> = s2.chars().collect();
v1.sort();
v2.sort();
println!("{}", v1 == v2);
}
medium
A. Runtime panic
B. true
C. Compilation error
D. false
Solution
Step 1: Convert strings to char vectors and sort
Both "listen" and "silent" are converted to vectors and sorted alphabetically.
Step 2: Compare sorted vectors
Sorted vectors for both strings become ['e', 'i', 'l', 'n', 's', 't'], so they are equal.
Final Answer:
true -> Option B
Quick Check:
Sorted chars equal means anagrams [OK]
Hint: Sort both strings and compare vectors [OK]
Common Mistakes:
Expecting false because strings differ
Confusing sorting with original order
Thinking println! prints vectors directly
4. Identify the bug in this Rust function that checks if two strings are anagrams: