fn is_anagram(str1: &str, str2: &str) -> bool {
// Check if the two strings have the same length.
if str1.len() != str2.len() {
return false;
}
// Create a vector of characters for each string.
let mut char_counts1 = Vec::new();
let mut char_counts2 = Vec::new();
for c in str1.chars() {
char_counts1.push(c);
}
for c in str2.chars() {
char_counts2.push(c);
}
// Sort the two vectors of characters.
char_counts1.sort();
char_counts2.sort();
// Check if the two vectors are equal.
for i in 0..char_counts1.len() {
if char_counts1[i] != char_counts2[i] {
return false;
}
}
// The two strings are anagrams.
return true;
}
fn main() {
let str1 = "hello";
let str2 = "olleh";
let is_anagram = is_anagram(str1, str2);
println!("The strings are anagrams: {}", is_anagram);
}
This program first checks if the two strings have the same length. If they do, then the program creates two vectors of characters, one for each string. The characters in each vector are then sorted. Finally, the two vectors are compared to see if they are equal. If they are equal, then the two strings are anagrams.
To run this program, you can save it as a .rs file and then compile and run it using the following commands:
Code snippet
rustc anagram.rs
./anagram
This will print the following output:
Code snippet
The strings are anagrams: true
This indicates that the two strings hello and olleh are anagrams.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Rust
This program first checks if the two strings have the same length. If they do, then the program creates two vectors of characters, one for each string. The characters in each vector are then sorted. Finally, the two vectors are compared to see if they are equal. If they are equal, then the two strings are anagrams.
To run this program, you can save it as a .rs file and then compile and run it using the following commands:
Code snippet
This will print the following output:
Code snippet
This indicates that the two strings hello and olleh are anagrams.