sub tilly_UCS { my $str = shift; # Try all cuts. Those that don't fall in 2 are repeats foreach my $char (split //, $str) { my @cut = split /\Q$char\E/, $str, -1; if (2 != @cut) { my @rejoined = map "$cut[$_-1]$char$cut[$_]", 1..$#cut; @rejoined = sort {length $b <=> $a} @rejoined; my @unique = tilly_UCS(shift @rejoined); foreach my $str (@rejoined) { if (length($str) < length($unique[0])) { last; # Avoid useless work, cannot improve } my @found = tilly_UCS($str); if (length($found[0]) > length($unique[0])) { @unique = @found; } elsif (length($found[0]) == length($unique[0])) { push @unique, @found; } } return @unique; } } # No repeats return $str; }