So I have a list:
a 25
b 18
c 18
d 18
e 14
f 14
g 12
... and so on
For each number that matches, I have to get every permutation of the identifier. The lists I would need from my example would be as follows:
abcdefg
abdcefg
acbdefg
acdbefg
adbcefg
adcbefg
abcdfeg
abdcfeg
acbdfeg
acdbfeg
adbcfeg
adcbfeg
Steps I am taking currently to solve the problem:
- Read in the list, place each value in an array ([a][12]) and place that in an ArrayList
- I then find if there are any repeating numbers and make note of it within a HashMap
- I then have a for loop set up to go through the HashMap. If the number didn't repeat, I just add it to the list. If the number did repeat, I use Heap's algorithm to get every permutation.
If I were to work through it, I would be adding the numbers to a list like this:
a
abcd
abdc
adbc
...
abcdef
abcdfe
abdcef
adbcfe
...
abcdefg
abcdfeg
My current code doesn't work as intended (generates only one list) and I'm not even sure how to begin writing a code that would continuously generate new lists, while also appending the old lists. I apologize for being lost, I'm in a data structures course right now and it all feels out of my familiarity zone (we just started discussing linked lists).
Note 1: allLists holds all the current lists (a, abcd, adcb,) and permutationList holds all the permutations of the list.
Note 2: I'm doing this through a boolean list, but used letters as an easy visual representation for what I'm trying to accomplish
Code where I expect the problem to be:
public static Boolean[] combine (int i, int j) {
int aLen = allLists.get(j).length;
int bLen = permutationList.get(i).length;
Boolean[] newList = new Boolean[aLen + bLen];
System.arraycopy(allLists.get(j), 0, newList, 0, aLen);
System.arraycopy(permutationList.get(i), 0, newList, aLen, bLen);
return newList;
}
public static void setAllLists() {
if(allLists.size() == 0) {
allLists.add(permutationList.get(0));
}
for(int i = 0; i < permutationList.size(); i++) {
for(int j = 0; j < allLists.size(); j++) {
Boolean[] newList = combine(i,j);
if(i == 0) {
allLists.set(j, newList);
}
else {
allLists.add(newList);
}
}
}
permutationList.clear();
}