I need to be able to tell, whether two lower-case strings differ at only one position, or they are the same. They must also be of the same length. That is
abab - abab is OK abab - abaa is OK abab - qrst is not OK abab - abba is not OK abab - ababa is not OKI need the fastest solution on Earth, since I have to do an enormous number of comparisions (for all two words of a fairly large dictionary). My solution is based on splitting the words into arrays and compare positionally, but it is painfully slow.
Could you make it much faster?sub compare{ return 0 unless length $_[0] == length $_[1]; return 1 if $_[0] eq $_[1]; my $diff = 0; my @l1 = split //, $_[0]; my @l2 = split //, $_[1]; for(my $i = 0; $i < scalar @l1; $i++){ $diff++ if $l1[$i] ne $l2[$i]; return 0 if $diff > 1; } return 1; }
In reply to Tell whether two strings differ at only one position by rg0now
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |