# Algorithm for calculating substitution combination with ordering

I need to calculate the combinations of elements with a substitution element.

For example for elements [A,B] if the substitution is X the results should be {[A,X],[X,B],[X,X]}. While this is easy enough to implement when the number of elements increase it becomes harder and error prone.

Is there an algorithm I can implement for this kind of combination calculations?

If this question is not a good fit for this site I do apologize, but this is the best site for an algorithmic question as far as I can tell.

EDIT :

I should have mentioned that all results should be of the same length as well.

• This is the same as picking a subset and replacing the elements in the subset with X. – Herp Derpington Oct 1 '13 at 9:55
• Make that an answer, I can work from there, thank you. – Thihara Oct 1 '13 at 10:27
• This seems more suitable for Computer Science which has a broader scope, for the scope of Theoretical Computer Science please check tour and help center. – Kaveh Oct 1 '13 at 18:02