in reply to Optimizing a string processing sub
Do a bitwise XOR of the words. There where they have the same letters, the resulting character will be a "\0". Next, count these occurrences.($word1 ^ $word2) =~ tr/\0//
As for the latter, even though it looks simpler as a requirement, as an implementation, it is not. So I'll take a completely different approach.
In English: cut the first word into characters. Put them in a bag. For each character in the second word, pick it out of the bag, at least, if there's at least one of it left in the bag. Count the number of characters taken from the bag.my %bag; my $total = 0; foreach(split '', $word1) { $bag{$_}++; } foreach(split '', $word2) { if($bag{$_}) { $bag{$_}--; $total++; } } return $total;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Optimizing a string processing sub
by spurperl (Priest) on Jan 09, 2003 at 06:00 UTC |