Suppose we say, for the sake of argument, that if two letters belong to the same group, we replace them by the first letter of the group, so we replace each occurrence of B by A and each occurrence of D by C.

Then, we start with the group ABCD, which gets translated into: AACC.

You could still have the following six permutations: AACC, ACAC, ACCA, CCAA, CACA, CAAC (if I did not forget any), not just two.

Again, this "translation" is just for the sake of discussion, it may be part of the final solution or may not be practical at all, or may not be what you need at all. For the time being, the only point in this "translation" is to understand better the requirement.

I assume that if any element belongs to same group then order doesn't meter like in previous case

if {A,B,C,D} , Group 1= A,B && group 2= C,D so any permutation order that only interchange intra group elements they are same, order within group doesn't meter and consider as a same order like {A,B,C,D} {A,B,D,C} both are same. Plus other thing i consider one group as one element {1,2} so 2!=2

Yes that is what i want. I have function where i submit the set and it will generate all permutations so if i only want these one and want to skip all other how my logic should be? Can any one show me in form of perl code?

No, as far as I understand your explanations, I have to disagree. Read again what I wrote in post #2, you have six possible permutations, not two.

If I summarize the letters with the group to which they belong, you have: {A,B,C,D} is 1122, and {C,D,A,B} is 2211.

{A,C,B,D} (or 1212), {A,C,D,B} (1221), {C,A,B,D} (2112) and {C,A,D,B} (2121) are in no case equivalent to your first two permutations, nor is any of them equivalent to any of the others. So I maintain that you have six possible permutations.

Or you have to explain an additional rule that you did not tell us about so far (or perhaps I missed some part of your explanation).

This is central to your task, so we really have to clarify this before we can go any further in trying to achieve what you want.

{A,C,B,D} (or 1212), {A,C,D,B} (1221), {C,A,B,D} (2112) and {C,A,D,B} (2121) are not allowed because they separate group elements. 22 always remain adjacent (As they belongs to same group) and same as 11. so there should only be {1122} {2211}

In my case initial set elements are always like

{1,1,2,2,2,3,3} initially 7! permutations

Here we have 3 groups with 2,3,2 elements inside that are sorted according to group value like group 1 elements come first then group 2 elements and then group 3 and so on. During permutation we can't allow permutation option that separate one group elements like {1,1,3,2,2,3,2} won't allowed We only allow permutations like {2221133} {3322211} in this case there are 3! permutations. I hope now you get what i want.

If the approach I described in post #5 meets your original requirement without any filtering, why are you even considering generating all possible permutations of all elements? Most of them would have to be declared invalid by the adjacency rule you explained to Laurent. Most of the rest would be eliminated because a redundant permutation has already been generated.

You did not say how many elements a group can have or how may groups you expect. The number of permutations which must be eliminated grows very rapidly. Consider five groups of three elements each. You want to keep only 5! of the 15! possible permutations. This notation hides the real size of the numbers. You would have to eliminate 1,307,674,367,880 of the 1,307,674,368,000, keeping only 120 permutations.

As you have already discovered, the filter is not simple. I cannot estimate the processing time for such a task, but I doubt that your boss would find it acceptable. Good Luck, Bill

I think you probably have to group items belonging to the same group together before you generate permutations.

You might have to do something similar to what is done in what is called Schwartzian Transform, i.e. first map your group of letters to a structure leading to a unique key for each group, then generate your permutation on the unique key, and then map back to the original value.

For example, if A and B belong to the first group, then map both {A,B} and {B,A} to 1 (1st group). Then use the unique key to figure out the permutations, and then map back to the original values.

Bill, my answer was really to the OP, not to you, even though the post title might lead to think differently.

To the OP, building on my idea to transform the data before calculating the permutations and transform them again afterwards to get back the original format, you might do something like this.

Assume you have {1,1,2,2,2,3,3}.

You might transform it into {1_1, 2_2_2, 3_3}. Then you just find all permutations of these three elements, which will give you six permutations. Then, in each permutation, you transform 1_1 back into {1,1}, and so on, so that if one of your permutations is {2_2_2, 1_1, 3_3), you just need to transform it back to: {2, 2, 2, 1, 1, 3, 3}.

This way, you only calculate the useful permutations, rather than calculating a large amount of permutations (7!, i.e. more than 5,000, in this case) and filtering them out afterwards