in reply to The sum of absolute differences in the counts of chars in two strings.

Assuming the strings are fairly short and contain a limited number of unique characters, which is likely in this case:

print diffCount('aaaaacaacaaagcc', 'acaggtgacaaaaaa'); sub diffCount { my (%counts, $sum, $i); $counts{substr($_[0], $_, 1)}++ for 0..(length($_[0]) - 1); $counts{substr($_[1], $_, 1)}-- for 0..(length($_[1]) - 1); $sum += abs $_ for values %counts; return $sum; }

I tried other methods using split, regex, etc., but this benchmarked the fastest. Obviously, doing the same thing in C or C++ would be even faster.

  • Comment on Re: The sum of absolute differences in the counts of chars in two strings.
  • Download Code