use strict; use Math::Combinatorics; my $firstLetter="c"; my $str="a?1,a?2,a%3,b?2,b?3,b%5,c%4,c%5,c?6,d%2"; my $substring = "a?,b%,c?"; my @combos = Math::Combinatorics::combine(4,split ",",$str); foreach my $combo (@combos) { ## check if numbers are all unique my $hash = {}; my ($count) = grep { ++$hash->{$_} > 1 } map { (split '')[2] } @$combo; next if defined $count; ## place $firstLetter with largest weight in first position ## ensure that substring strings are contained within the combination }