Given is a set M
with some elements, e.g. M={a, b}
and another set F
with specific numbers representing the quantity in the permutations of M
. e.g. F={3, 1}
, so the permutations are: P={(a, a, a, b), (a, a, b, a), (a, b, a, a), (b, a, a, a)}
for this example.
I want to print those but after fiddling around with recursion I still can't get it to work. My current code is like:
void printPermutations(Map<Integer, String> m, List<Integer> f, List<Integer> cf, int size, int depth, int index, int innerindex, List<String> toPrint)
{
if(depth == size)
{
System.out.println("");
for(String a : toPrint)
System.out.print(a + " ");
System.out.println("");
}
else
{
for(int i = index + 1; i < m.size(); ++i)
{
if(f.get(innerindex).compareTo(Integer.valueOf(1)) < 0)
++innerindex;
toPrint.add(m.get(innerindex));
f.set(innerindex, f.get(innerindex) - Integer.valueOf(1));
printPermutations(m, f, f, size, depth + 1, i, innerindex, toPrint);
f.set(innerindex, f.get(innerindex) + Integer.valueOf(1));
toPrint.remove(m.get(innerindex));
if(f.get(innerindex).compareTo(cf.get(innerindex)) == 0 && innerindex > 0)
--innerindex;
}
}
}
//call
//asume some cool elements in f and mAsSet
// f.size() == mAsSet.size()
List<String> toPrint = new ArrayList<String>();
Map<Integer, String> m = new HashMap<Integer, String>(mAsSet.size());
int counter = 0;
for(String a : m)
m.put(Integer.valueOf(counter++), a);
int size = 0;
for(Integer i : f)
size += i;
printPermutations(m, f, f, size, 0, -1, 0, toPrint);
I don't really see whats wrong here, it prints exactly nothing. Of course I debugged this, but I really do not have any other idea to kindof archieve what I want.