Let's say I have two lists:
l1={1,2}
l2={a,b}
I want to find legal permutations of combining these two list in a way that the ordering of the members of each list, is preserved. In other words I am looking for a function ConditionalPermutation such that:
ConditionalPermutation[{1,2},{a,b}]=
{{1,2,a,b},{1,a,2,b},{a,b,1,2},{a,1,b,2},{1,a,b,2},{a,1,2,b}}
as you can see, in this simple case, we have 4 elements altogether, so the total number of permutations are 4*3*2=12. Out of these half have 2 appearing before 1 and also half have b appearing before a. Therefore the list of interest has (24/2)/2=6 legal permutations. In general there could be multiple l_i lists and each would have a different size.
I can code it with an imperative implementation, but I am looking for a neater way of doing it in Mathematica.
Ideally I want the solution to accept a general condition, or potentially one for each input list.