I have two strings, and I would like to test if they are anagrams of each other.
To test if string A is an anagram of string B, the characters of A and B are both sorted. If the resulting sorted strings match exactly, string A and string B are anagrams of each other.
I am spliting the strings up into character arrays, using Perl’s sort routine, joining the characters back together, and testing for string equality with eq:
sub anagram
{
my ($s1, $s2) = @_;
return (join '', sort { $a cmp $b } split(//, $s1)) eq
(join '', sort { $a cmp $b } split(//, $s2));
}
Is there a way to avoid having to convert between the scalar and array types (relying on join and split)? And if so, which method is more efficient?
If both strings are variable, I don’t think you can do much better. One alternative is to build a hash that maps characters to their counts, and then compare that the hashes have the same keys and values. I believe that this is O(n) instead of O(n log n) for your approach, but it would probably have worse actual performance except on very long strings.
If you want to compare variable strings to a fixed reference string, then perhaps the hash-based approach might pay dividends earlier, since you only need to hash the reference once.