If the used alphabet is known and most of the letters are likely to be used and the strings are not too long, I think that it would be worthwhile to investigate tr/// over split+count, potentially by generating one huge eval string:
my $code = join "\n;\n", map {sprintf '$count{ $_ } = tr/%s/%s/', $_, +$_ } @alphabet; my %count; local $_ = $string1; eval $code;
The second string counting could be rolled into a second eval:
my $code2 = join "\n+\n", map {sprintf 'abs($count{ %s } - tr/%s/%s/)' +, $_, $_, $_ } @alphabet; local $_ = $string2; eval $code2;
But as the tr-approach reads the strings multiple times, this will really depend on the length of the strings and the size of the alphabet.
In reply to Re: The sum of absolute differences in the counts of chars in two strings.
by Corion
in thread The sum of absolute differences in the counts of chars in two strings.
by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |