http://www.perlmonks.org?node_id=1019812

supriyoch_2008 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks,

I have a string $a= “L1 H1 L2 H2”; with two Ls at positions 1, 3 and two Hs at positions 2, 4, respectively. The Ls can interchange between them i.e. say for position 1 , L1 may change to L2 but not to H. Similarly, the two H can interchange between them at positions 2 & 4. Finally these four elements will produce a set of 4 sequences like L1H1L2H2; L2H1L1H2; L1H2L2H1; L2H2L1H1; taking the fixed positions for L (1,3) and H(2,4) into consideration. I am at my wit’s end to write a script which can find all these four combinations. The given string just consists of 4 positions and two groups i.e. L & H each with 2 elements and so it can be worked out easily. If the string contains, say 500 positions with 10 main groups and each group with varying number of elements, it will be much easier to find all the possible combinations using a perl script. I have gone through the posts relating to permutations, combinations and AoA but I couldn’t find a text which can solve this problem. I am looking forward to the suggestions of Perl Monks regarding this problem. The results might look like:

Number of Combinations: 4 Combinations are: L1H1L2H2 L2H1L1H2 L1H2L2H1 L2H2L1H1