Here's a bit-logic based approach:
#!/usr/bin/perl -w
use strict;
my %groups = (
group1 => ['cooling', 'rooting', 'hooting', 'looking', 'doormat',
+'cooking', 'cookies', 'noodles'], # desired result "-oo----"
group2 => ['yourself', 'southern'], # desired result "-ou--e--"
group3 => ['arden', 'arlen', 'asked'] # desired result "a--e-"
);
sub show_bits {
#print unpack("B*", shift), "\n";
}
for my $g (values %groups) {
my $w1 = $g->[0];
my $and = "\xff" x length($w1);
my $or = "\0" x length($w1);
for my $w (@$g) {
print "$w\n";
#print join(" " x 7, split //, $w), "\n";
show_bits($w);
$and &= $w; show_bits($and);
$or |= $w; show_bits($or);
}
my $xor = $and ^ $or; show_bits($xor);
$xor =~ tr/\0/\xff/c; show_bits($xor);
my $mask = ~$xor; show_bits($mask);
my $common = $w1 & $mask;
$common =~ tr/\0/-/;
print "$common\n";
my $i = -1;
print map {$i++; $_ ne "-" ? "$_ : $i\n" : ()} split //, $common;
print "\n";
}
__END__
yourself
southern
-ou--e--
o : 1
u : 2
e : 5
cooling
rooting
hooting
looking
doormat
cooking
cookies
noodles
-oo----
o : 1
o : 2
arden
arlen
asked
a--e-
a : 0
e : 3
Uncomment the commented out prints to see how the bit
strings are being transformed/combined...
(Update: fixed cut-n-paste error $and ^= $or — thanks johngg)