#!/usr/bin/perl use strict; use warnings; my (%seen, %signatures); my @words = qw / ba ab aabb aaabbb xxyy xxxyyy baaab baba xxy xyz ABBA dcdccd cd/; for my $word (@words) { chomp $word; # not needed here, but may be useful when reading from a file or a DATA section my $word = lc $word; next if $seen{$word}++; next if $word =~ /(.).*\1/; # skip if the word has repeated letters my $signature = join "", sort split //, $word; push @{$signatures{$signature}}, $word; } for my $word (@words) { chomp $word; my $word = lc $word; next unless $word =~ /(.).*\1/;; # skip words already stored (having no duplicate letters) my %unique_letters = map { $_ => 1 } split //, $word; # could use the uniqstr function of Lists::Util my $signature = join "", keys %unique_letters; push @{$signatures{$signature}}, $word if exists $signatures{$signature}; } for my $key (keys %signatures) { if (scalar @{$signatures{$key}} > 1) { print "@{$signatures{$key}}\n" } } #### ba ab aabb aaabbb baaab baba abba cd dcdccd