2022-04-08 12:59:10 +00:00
|
|
|
pub fn is_anagram(a: String, b: String) -> bool {
|
|
|
|
// Both strings must have the same length otherwise they can't be anagrams.
|
|
|
|
if a.len() != b.len() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort both strings' characters so they're in alphabetical order.
|
|
|
|
let mut a = a.chars().collect::<Vec<_>>();
|
2022-04-08 21:00:38 +00:00
|
|
|
a.sort_unstable();
|
2022-04-08 12:59:10 +00:00
|
|
|
|
|
|
|
let mut b = b.chars().collect::<Vec<_>>();
|
2022-04-08 21:00:38 +00:00
|
|
|
b.sort_unstable();
|
2022-04-08 12:59:10 +00:00
|
|
|
|
|
|
|
// And if they are equal, then they're anagrams.
|
|
|
|
a == b
|
|
|
|
}
|