#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
# Filter out all words that can't be candidates one way or the other
my @candidates=grep { m{[RS]} } <DATA>;
chomp(@candidates);
# Sort them in order of length
my @orderedCandidates=sort{ length $a <=> length $b } @candidates;
# Push a guard on the end
push @orderedCandidates,"";
warn Data::Dumper->Dump([\@orderedCandidates],[qw(*orderedCandidates)]
+),' ';
my %hash;
my $length=length($orderedCandidates[0]);
for my $word (@orderedCandidates) {
if (length($word) == $length) { # Same length ... add it to the ha
+sh
$hash{$word}=undef;
}
else { # Hash is complete
for my $Rword (grep { m{R} } keys %hash) { # Word has a R
my $pos=0;
while (($pos=index($Rword,'R',$pos)) >= 0) { # Found an R
# Make its S equivalent
my $Sword=$Rword;
substr($Sword,$pos,1)='S';
print "$Rword - $Sword\n"
if (exists $hash{$Sword});
$pos++
}
}
# Done with this hash --- so start over
%hash=(); $hash{$word}=undef;
$length=length($word);
};
}
__DATA__
ONE
THREE
FOUR
FOUS
RRRRRRRR
RSRRRRRS
RRSRRRRR
which yields
FOUR - FOUS
RRRRRRRR - RRSRRRRR
Just for fun I modified this replacing R by each of the letters of the alphabet and S by another (using english-words.95 (~ 220,000 words).
The R <=> S exchange is the most common with 381 words and the J <=> Q is the least common with only 5 words.
Elapsed time for the run 344.65 seconds.